1

I currently have a web page where I am displaying the date a file was created as a string. Since the file is being generated on an american server, the date is in UTC.

An example date would be: 6/3/2016 10:13:55 AM

I am wanting to convert this date to the timezone of the user who visits the webpage. The web page can be visited by people all over the world.

For example, I would like the example time to display 6/4/2016 01:13:55 AM for Australian Eastern Standard Time (AEST).

Johnrad
  • 2,637
  • 18
  • 58
  • 98
  • How are you getting the file's created date? – IMTheNachoMan Jun 03 '16 at 20:16
  • Through a method in my c# helper class. . . `var directory = new DirectoryInfo(path);`, `var astWriteTime = directory.CreationTimeUtc;`. This entire time I thought it was in CDT time, but I forgot I got the creation time in UTC. – Johnrad Jun 03 '16 at 20:27
  • 1
    This question has been asked many times before. See the duplicate posted, which is the same answer given by gravityplax below. Also, you might look into [moment.js](http://momentjs.com) if you have formatting concerns, or other time zone issues. Cheers! – Matt Johnson-Pint Jun 03 '16 at 20:43
  • Noting your comment here about C#, getting the creation time in UTC is great. Just don't send it out in a locale-specific format. Send it in ISO8601/RFC3339 format. See [the W3C recommendation on this](https://www.w3.org/TR/NOTE-datetime). Use `.toString("o")` in .NET. (Any good JSON or XML serializer should also be using that format.) – Matt Johnson-Pint Jun 03 '16 at 20:46

1 Answers1

2
var ms = Date.parse('6/3/2016 10:13:55 AM' + ' UTC');
var date = new Date();
date.setTime(ms);
var timezone_shifted_string = date.toLocaleString();
Jake Haller-Roby
  • 6,335
  • 1
  • 18
  • 31
  • Will this work if my local is not en-us? – IMTheNachoMan Jun 03 '16 at 20:16
  • Holy smokes was I over complicating this. – Johnrad Jun 03 '16 at 20:20
  • 1
    It will default to the timezone of the local runtime, but you can also override it manually to any timezone: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString – Jake Haller-Roby Jun 03 '16 at 20:21
  • @gravityplanx I accidentally misinterpreted my original time I received, I am getting it in UTC time by default. I have modified my question. I assume I would just remove the concatenation of `' CDT'`. – Johnrad Jun 03 '16 at 20:28
  • Local timezone will be used if one is not provided. Just change `CDT` to `UTC`. – Jake Haller-Roby Jun 03 '16 at 20:31
  • There are many things wrong with this approach. 1) `6/3` could be interpreted as June 3rd or March 6th, depending on the current locale. 2) `CDT` is only applicable for part of the year when daylight saving time is in effect, so it can't be applied to every date input. 3) Using `CDT` only works in some implementations, its not part of the ECMAScript spec. 4) Time zone abbreviations can be ambiguous. – Matt Johnson-Pint Jun 03 '16 at 20:31
  • @MattJohnson: The question specified that input was coming as a string in ~CDT~ *UTC* consistently. You're right that this approach has issues if you're trying to convert dynamically, but all of your problems disappear if the data is stored as a string instead of a date object, which is how I interpreted things. – Jake Haller-Roby Jun 03 '16 at 20:33
  • I'm not sure what you mean by that. You're using a `Date` object here, and you're relying on `CDT` to be consistently interpreted as UTC-5. That's not guaranteed. You're also assuming that the output format of `toLocaleString` will match the given input format, but that all depends on which locale it runs in. – Matt Johnson-Pint Jun 03 '16 at 20:37
  • @MattJohnson I also flubbed up my question initially, I meant UTC not CDT. The date comes in initially as a string (passed in via a view model to the view) which displays the date a file was modified in UTC time. – Johnrad Jun 03 '16 at 20:39
  • 1
    Oh - that changes things entirely. However you should still be careful with the formatting part. And note that the middle bits are not necessary, as you can just pass the string into the `Date` constructor, rather than calling `Date.parse`. Do be careful about string input format though. My point about M/D/Y vs D/M/Y is still valid. – Matt Johnson-Pint Jun 03 '16 at 20:41
  • 1
    @MattJohnson Ah, sorry, misinterpreted you then. As far as formatting goes, if the output format needs to be controlled, it can be via additional optional fields. – Jake Haller-Roby Jun 03 '16 at 20:42