locutusjs / locutus

Bringing stdlibs of other programming languages to JavaScript for educational purposes

Home Page:https://locutus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to set timezone in locutus date function?

qwe0001 opened this issue · comments

I've read date.js and ini_set.js source code already.

https://github.com/kvz/locutus/blob/a5e9fa0fb8bcfa50ccbd7713df60a64c1122bc0b/src/php/datetime/date.js
https://github.com/kvz/locutus/blob/master/src/php/info/ini_set.js

But I couldn't understand how to set timezone cuz I am a yellow monkey u know.

How to set specified timezone?

Temporarily, I set timezone like this.

var PHP = require('locutus/php');

function getNow(myDate)
{
  var now;
  var jpnTime;

  if(! PHP.var.empty(myDate) && PHP.var.is_numeric(myDate)){
    jpnTime = new Date(myDate).toLocaleString("en-US", {timeZone: "Asia/Tokyo"});
  }
  else {
    jpnTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Tokyo"});
  }

  now = PHP.datetime.date('Y-m-d H:i:s', PHP.datetime.strtotime(jpnTime));

  return now;
};

// Run on this in Amazon AWS Lamba@Edge, Region is us-east-1
console.log(new Date()); // timezone is UTC? e.g. "2020-02-14T08:04:00.855Z"
console.log(getNow()); // timezone is Asia/Tokyo correctly e.g. "2020-02-14 17:04:00"

I know that I can set timezone with moment-timezone.
But I wish to NOT use any other libraries.

Because Lambda@Edge has a source code capacity limit,
And the limit is quite strict.

So I want to avoid using external libraries as much as possible.
Any Ideas?

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 14 days.