0

Thought I understood how to convert a time stored in our DB to the users local time but seems I do not. When I save the kick off time into my postresql database (On Heroku) it is supplied as 2018-11-14 19:45:00, I save it as a TIMESTAMP. At the time i save it should I use moment?

Running Heroku logs I have noticed that the server time is 2018-11-14T19:45:00.200076+00:00, which is UTC ?

Using moment JS in my node app I am carrying out the following

fixture.kick_off = 2018-11-14 19:45:00.000000
<%=  moment(fixture.kick_off).local().format('HH:mm A') %>

I have had a user from Denmark state that this is showing as 19:45 pm, where as i wanted it to show 20:45 PM as per the time difference

Have I misunderstood something here (very likely)

Thanks

Update

I am now using the below as per Matt's answer

<%=  moment.utc(fixture.kick_off).local().format('HH:mm A') %>

Following on from Matt's comment around checking the typeof fixture.kick_off, it was returning object, have now updated my code to be

<%  var kick_off = fixture.kick_off.toString() %>
<%=  moment.utc(kick_off).local().format('HH:mm') %>

Which returns the following in the console

Deprecation warning: value provided is not in a recognized RFC2822 or ISO 
format. moment construction falls back to js Date(), which is not reliable 
across all browsers and versions. Non RFC2822/ISO date formats are discouraged 
and will be removed in an upcoming major release. Please refer to 
http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: Thu 
Nov 15 2018 19:00:00 GMT+0000 (Greenwich Mean Time), _f: undefined, _strict: 
undefined, _locale: [object Object]
Error
Richlewis
  • 15,070
  • 37
  • 122
  • 283

1 Answers1

0

Assuming the time stored in your database based on UTC, you simply forgot to tell moment that. Use moment.utc(input) instead of moment(input)

moment.utc(fixture.kick_off).local().format('HH:mm A')
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • I knew you would come to my rescue Matt :-) I have just finished reading this too (typical i found it after asking the question) https://stackoverflow.com/questions/32540667/moment-js-utc-to-local-time thank you for taking the time to answer, just trying this out now – Richlewis Nov 14 '18 at 22:22
  • hmm, still not working apparently, maybe my issue stems from elsewhere then, Ill investigate and update my question – Richlewis Nov 14 '18 at 22:33
  • Looking at your edit, the question is a but murky now. If the UTC time is 22:37, then the time in Denmark (UTC+1 on that date) would be 23:37, but you're asking for 20:45? Can you please clean up your question to be consistent? – Matt Johnson-Pint Nov 14 '18 at 22:44
  • apologies, I have cleaned it up now, the Heroku server time is the same as here in the UK. If this question is still murky, happy to delete it and start again – Richlewis Nov 14 '18 at 22:48
  • Keep in mind that the UK isn't always aligned to UTC. It does UTC+1 for BST. But I don't think that's the source of your problem. Probably multiple things compounding here. I recommend doing some debugging and make sure that `fixture.kick_off` is really a string with a value like you showed here (you're missing quotation marks though - maybe you don't have a string but something else, like a `Date` object?) – Matt Johnson-Pint Nov 14 '18 at 23:05
  • Oh, aside, but one typically doesn't use AM/PM with a 24-hour clock. "20:45" (`HH:mm`) or "8:45 PM" (`h:mm A`) make sense, but "20:45 PM" doesn't really. – Matt Johnson-Pint Nov 14 '18 at 23:06
  • You are correct that my date is not a string, but an `object`, if I try and convert to a string I get the `Deprecation warning: value provided is not in a recognized RFC2822 or ISO format` error. Next step is looking into this – Richlewis Nov 14 '18 at 23:30
  • Yes, you need to know how that object was created in the first place. – Matt Johnson-Pint Nov 15 '18 at 01:41