4

I have Javascript based app (using Nativescript) in which I'm using moment to do some time manipulation. My server returns dtae times to me in the following string format "9/14/2016 4:52:20 PM" Which are Not in UTC. Depending on where the user is I would like to change the time stamp of this string to match their time zone. So I'm trying to get a string that would look like this "4:52 PM" in EST or "12:52 PM" if it's pacific timezone. I'm currently trying to do this using MomentJS like in the following way:

exports.dateTimeStamp = function(value) {
    var utcDate = moment.utc(value);
    var localDate = moment(utcDate).local();
    var formatedDate = moment(localDate).format('LT');
    return formatedDate;
}; 

Unfortunately this isn't working correctly. As of now If I try to do this and the user is in ****EST (GMT-4:00)**** and the string value I pass in is equal to "9/14/2016 4:52:20 PM" I'm getting back "12:52 PM" Rather than "4:52 PM" Could some one point out what I'm doing wrong? My thought is in involves the switch to UTC but that seems like it's necessary from what I have read.

EDIT I've looked into Moment Timezone, but I don't think that will work since my application is global and it seems Timezone requires a hard coded country/city string to be passed into it. In my case Javascript being used in an Android App. So I get the locale and culture info from the device. Maybe I'm just not quite understanding how to use it.

Emil Oberg
  • 4,006
  • 18
  • 30
Stavros_S
  • 2,145
  • 7
  • 31
  • 75
  • 1
    What is the reference time zone of the value your server is giving you? In other words, is `9/14/2016 4:52:20 PM` in UTC, or in Eastern time? – Matt Johnson-Pint Sep 15 '16 at 02:58
  • And also, are you sure you're getting back "1:52 PM" and not "12:52 PM"? – Matt Johnson-Pint Sep 15 '16 at 03:00
  • Oh, and EST is UTC-5. Since DST is in effect on these dates, you would say EDT to align with UTC-4. – Matt Johnson-Pint Sep 15 '16 at 03:01
  • @MattJohnson Sorry, my confusion, yes I'm wanting to respect DST. Actually I think I was getting back 12:52 PM. – Stavros_S Sep 15 '16 at 03:04
  • @MattJohnson the time is actually stored in the DB as '2016-09-14 16:52:20.0000000' then the server sends down the datetime string in the format mentioned in the question. In this case it would be in EST I believe. – Stavros_S Sep 15 '16 at 03:10
  • Well there's your problem then. You should usually store UTC times in the database. See http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices – Matt Johnson-Pint Sep 15 '16 at 03:11
  • @MattJohnson Ok so in that case the UTC would be '2016-09-14 20:52:20.0000000' , right? If that's the case we're storing both the UTC and the relative time I believe. – Stavros_S Sep 15 '16 at 03:13
  • Well, then send the UTC time down to the client, and your code should work. Sure, it could be optimized a bit, and you should avoid using locale-specific formats without specifying the format tokens, but other than that the problem is just that you're passing local values and asserting that they're UTC. – Matt Johnson-Pint Sep 15 '16 at 03:14
  • See also https://maggiepint.com/2016/05/14/moment-js-shows-the-wrong-date/ – Matt Johnson-Pint Sep 15 '16 at 03:15
  • @MattJohnson Ok I just confirmed that unfortunately neither date time is actually UTC. So there goes that plan :( – Stavros_S Sep 15 '16 at 03:18
  • Actually I guess on the server side (.net) I could convert the initial datetime string to a utc string before sending it down to the client. – Stavros_S Sep 15 '16 at 03:23
  • Be careful. if you just do `.ToUniversalTime()` you'll be inferring the local time zone setting of the server. Better to know explicitly what time zone you're working with and convert with `TimeZoneInfo` or Noda Time. – Matt Johnson-Pint Sep 15 '16 at 03:32
  • Hmm in that case I wouldn't be able to do that since I wouldn't know the timezone at that point. Timezone is only know once the data has made it to the device... – Stavros_S Sep 15 '16 at 03:35
  • 1
    If you want to convert a date from one time zone to another, then the conversion must know the source and destination time zones. A date string like "9/14/2016 4:52:20 PM" does not have a time zone, so it will be assumed to be "local" by default by most (all?) parsers. Similarly, most formatters will assume the local (i.e. host) time zone is required for output unless something else is specified. It's common to use UTC as the source as that removes one unknown in a standardised way. – RobG Mar 16 '17 at 22:45

0 Answers0