17

Hi all im looking for a way to get current system time in a timestamp. i used this to get timestamp:

new Date().getTime();

but it return the time in UTC timezone not the timezone that server use

is there any way to get timestamp with system timezone?

user1229351
  • 1,985
  • 4
  • 20
  • 24

3 Answers3

19

Check out moment.js http://momentjs.com/

npm install -S moment

New moment objects will by default have the system timezone offset.

var now = moment()
var formatted = now.format('YYYY-MM-DD HH:mm:ss Z')
console.log(formatted)
Noah
  • 33,851
  • 5
  • 37
  • 32
8

Since getTime returns unformatted time in milliseconds since EPOCH, it's not supposed to be converted to time zones. Assuming you're looking for formatted output,

Here is a stock solution without external libraries, from an answer to a similar question:

the various toLocale…String methods will provide localized output.

d = new Date();
alert(d);                        // -> Sat Feb 28 2004 23:45:26 GMT-0300 (BRT)
alert(d.toLocaleString());       // -> Sat Feb 28 23:45:26 2004
alert(d.toLocaleDateString());   // -> 02/28/2004
alert(d.toLocaleTimeString());   // -> 23:45:26

And extra formatting options can be provided if needed.

Community
  • 1
  • 1
ScottyC
  • 1,467
  • 1
  • 15
  • 22
7

Install the module 'moment' using:

npm install moment --save

And then in the code add the following lines -

var moment = require('moment');
var time = moment();
var time_format = time.format('YYYY-MM-DD HH:mm:ss Z');
console.log(time_format);
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53