0

I have UTC date variable:

let d = '/Date(1538560800000+0200)/';

I tried to convert this to my local time, without success:

moment(accessGroup.TimeOn).toDate() // result: Wed Oct 03 2018 12:00:00 GMT+0200 (Central European Summer Time)
moment(d).local().format("DD-MM-YYYY hh-mm-ss"); // result: "03-10-2018 12-00-00"
moment.utc(d).local().format("DD-MM-YYYY hh-mm-ss"); // result: "03-10-2018 12-00-00"

I want to get my local time 14:00:00. What is the proper way to do that ?

Milos
  • 1,678
  • 4
  • 23
  • 49

2 Answers2

1

When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.

Moment accepts the ISO 8601 and RFC 2822 formats for string parameters, and your input /Date(1538560800000+0200)/ is neither. Still, when you pass this to moment, it tries to convert this to a string and ends up extracting the possible timestamp parameter, which is 1538560800000. This can be seen in the following code, as both end up as the same values ..

let d = '/Date(1538560800000+0200)/'
moment(d).toString() // Wed Oct 03 2018 12:00:00 GMT+0200
moment(1538560800000).toString() // Wed Oct 03 2018 12:00:00 GMT+0200

1538560800000 when converted to GMT is Wed Oct 03 2018 10:00:00 GMT+0000, hence the output of the following code ..

moment.utc(d).toString() // Wed Oct 03 2018 10:00:00 GMT+0000

So, under no circumstance you're ever going to get the time as 14:00:00 as this timestamp does not represent that. And timestamps are always absolute (in GMT), there is no such thing as a local timestamp.


I feel the gap here is your wrong interpretation of the input string, you're thinking ..

  • Convert 1538560800000 to local datetime, which is 3rd October, 12:00:00 (local)
  • Add +0200 hours to it, which means turn it to 3rd October, 14:00:00

But, the correct way to read it is ..

  • Convert 1538560800000 to datetime, which is 3rd October, 10:00:00 (absolute/GMT)
  • +0200 represents the timezone of the source which is GMT+0200.

Again, the first interpretation is wrong for the reason mentioned above - unix timestamps are always in GMT, they are never in local timezones.

rmn
  • 1,119
  • 7
  • 18
0

I have created jsfiddle as well: http://jsfiddle.net/csinghal/dp7rzmw5/34811/

and same code as below for all format:

 var localDate = new Date(1538560800000+0200);
 var utcFormat = moment(localDate).utc().format('YYYY-MM-DD HH:MM:SS');
 var localFormat = moment.utc(utcFormat, 'YYYY-MM-DD 
 HH:MM:SS').local().format('MM/DD/YYYY hh:mm a');
 console.log('LocalDate: ', localDate);
 console.log('utcFormat: ', utcFormat);
 console.log('localFormat: ', localFormat);
  • This works, for your example, but how to do that for my UTC time "/Date(1538560800000+0200)/" – Milos Oct 04 '18 at 11:37
  • No, I get result: '10/03/2018 12:00 pm', I should get '14:00' since '/Date(1538560800000+0200)/' is UTC time. – Milos Oct 04 '18 at 14:15