0

I'm having issues with Epoch Date conversion. I am using moment.js JavaScript library for this purpose. I have a UTC DateTime value in the Epoch format. When I run the same epoch conversion code on two different computers (using different timezones) I get different date conversion outcomes.

I am using the following JavaScript code:

 var epochTime = 1603825239000;

 var result = moment(epochTime).format('lll'); 

 document.write(result); // the result I get is: Oct 28, 2020 6:00 AM

Now when running the exact same code on a different computer having the Pacific timezone, it returns the result as:

  var epochTime = 1603825239000;

  var result = moment(epochTime).format('lll'); 
 
  document.write(result); // now the result is different: Oct 27, 2020 12:00 PM

Is there something I could do to ensure that the epoch date conversion result doesn't change regardless of the computer timezone? and I always get the first conversion value i.e. Oct 28, 2020 6:00 AM

theITvideos
  • 1,462
  • 2
  • 18
  • 29
  • `1603825239000` == `"2020-10-27T19:00:39.000Z"`. Anything else is reflecting conversion to local time - which of course will be different in different time zones. Which time zone are you *trying* to get? Also, see https://codeofmatt.com/please-dont-call-it-epoch-time/ – Matt Johnson-Pint Oct 29 '20 at 16:55
  • Hi Matt, I am trying to get a UTC time because in SQL Server the UTC timestamp is stored as "2020-10-28 06:00:39.997". I have a WCF which returns the time in Epoch format with the value "1603825239000" – theITvideos Oct 29 '20 at 23:37
  • If the database is indeed in UTC time, and it is indeed 6 am, then you've converted it to the Unix timestamp incorrectly within your WCF component. Likely you've introduced the local time zone of the computer where that code is running. The timestamp you're showing is not 6am UTC, it's 7pm UTC the day prior. I suggest checking your .NET code carefully, and perhaps ask a new question showing that code. – Matt Johnson-Pint Oct 30 '20 at 01:54
  • Hi Matt, I checked backend code and I can see that it is converting the original UTC to the server's local TimeZone and returning the JSON date as: "/Date(1603825239000+1100)/" It's adding 11 hours to the original UTC date. Is there a way I could convert this back to the original UTC date in JavaScript or moment.js? – theITvideos Oct 30 '20 at 04:25
  • 1
    The problem isn't in your JavaScript, but clearly in your C# code. You need to use something like `yourDateTime = DateTime.SpecifiyKind(yourDateTime, DateTimeKind.Utc)` after you populate it from the database, *before* you serialize the result. – Matt Johnson-Pint Oct 30 '20 at 16:42
  • Thanks Matt, In my WCF service, I have a JSON data returned from a Web api and I deserialize it to convert to an object. I am using the DateTime.SpecifiyKind method like so : https://dotnetfiddle.net/iZ6OcC# do you think there a better way to do it? – theITvideos Oct 31 '20 at 01:50
  • 1
    It would be better if the json had a `Z` at the end of the timestamp (Z = UTC). Then you wouldn't need to specify the kind - Newtonsoft would do it for you. But if indeed you are specifying kind as you showed in the sample, your WCF output (which you didn't show) would have `+0000` on it, not `+1100`. So somewhere you are losing the kind. Debug and check the `.Kind` property along the way. Also, we're quite far off topic from where this thread started. You might do better to ask a new question focused on the C# part of your code (including WCF). – Matt Johnson-Pint Oct 31 '20 at 18:27
  • Thanks for the response Matt. After applying the DateTime.SpecifiyKind changes in C# I can see that in my WCF response, it doesn't do any server TimeZone conversion and I get the values as /Date(1603864839000)/ . Also in C# i can see the value as Utc in the Kind property as shown as: https://imgur.com/a/IAxchBk – theITvideos Nov 01 '20 at 03:24
  • Glad I could help. – Matt Johnson-Pint Nov 02 '20 at 00:36

1 Answers1

0

You can use moment timezone to specify the timezone you wish to display the time in.

Or, we can do moment.utc() to create a date set to the UTC timezone.

const epochTime = 1603825239000;
const timeZone = "America/Los_Angeles";

const date = moment.tz(epochTime, timeZone);
console.log("Time in timezone:",date.format('lll'))

// Or just write UTC time
console.log("UTC time:",moment.utc(epochTime).format('lll'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data-10-year-range.js"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • Thanks Terry for replying. I can now see what is happening at my end. My dev server is actually on a different timezone than the production server. The production server returns the JSON DateTime as "/Date(1603864839000+0000)/". Whereas, my dev server (being on different timezone) returns the exact same time as "/Date(1603825239000+1100)/" i.e. adding 11 hours to the original time. Is there a way I can factor in that +1100 or subtract that 11 hours and get the actual original date using moment.js or JavaScript? – theITvideos Oct 30 '20 at 02:05
  • @theITvideos—see [.NET date, Moment.js, UTC and Timezone shifting](https://stackoverflow.com/a/48509480/257182). No library required. – RobG Oct 30 '20 at 05:59