0

This has been making me nuts all day. I understand that JS Date() will set to local time, and thus, make it appear that it's been bumped one day in the past. However, all formatting with date-fns requires a date object, at least as far as I can tell, and it's just plain frustrating to simply format a date, stored ONLY as a date type in Postgres. I'm an inch away from simply parsing and formatting the correct UTC date myself and ditching date-fns.

Date is stored like so in pg: 2022-11-08
I'm formatting like so, on the front-end: format(new Date(job.active_range_start), "MMM dd, yyyy")
The value of job.active_range_start is: '2022-11-08T00:00:00.000Z'
What I get after formatting w/ date-fns: 'Nov 07, 2022'
But new Date(job.active_range_start) results in this: Mon Nov 07 2022 19:00:00 GMT-0500 (Eastern Standard Time)

How can I simply use date-fns format() (or whatever) to format the correct UTC date onto the screen? I tried parse(), parseISO(), parseJSON() and all end up giving me the non-UTC localized datetime.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Tsar Bomba
  • 1,047
  • 6
  • 29
  • 52
  • @Dai Yes, don't have the luxury of removing date-fns from a very large codebase, and don't want devs using two different approaches. – Tsar Bomba Nov 18 '22 at 00:15
  • @Dai I'm really looking for the answer in date-fns at this time. Is it not possible? – Tsar Bomba Nov 18 '22 at 00:19
  • @Dai Feel free to post your answer in Intl - someone will want to know. Thanks. – Tsar Bomba Nov 18 '22 at 00:22
  • Either hack the timezone or use date-fns-tz extension. https://stackoverflow.com/a/52352512/1231844 – OFRBG Nov 18 '22 at 00:23
  • @OFRBG I was afraid someone was going to say something like "hack the date-fns lib". To me, it renders date-fns `format()` worthless, as it'd be easier to get format I want without date-fns. Thanks for the link. – Tsar Bomba Nov 18 '22 at 00:28
  • No, not hack the date-fns library. You can either hack the timezone and manually shift it to make it look like you're in UTC OR use the date-fns-tz official extension. – OFRBG Nov 18 '22 at 00:31
  • @OFRBG Right, yeah, I get what you meant. I just tried `format()` in date-fns-tz and the same thing happens, since I have to pass it a `Date()` object, regardless. Am I missing something? – Tsar Bomba Nov 18 '22 at 00:39
  • @Dai It's a string, not a Date, like date-fns format() requires. – Tsar Bomba Nov 18 '22 at 00:41
  • @Dai That value is a string and cannot be passed to date-fns format(). I'd have to format manually or just use another lib, which is what I suspected anyhow. – Tsar Bomba Nov 18 '22 at 00:45
  • @Dai Here is the output of what you suggest, which is not the format I need to display: '2022-11-08T00:00:00.000Z' – Tsar Bomba Nov 18 '22 at 00:49
  • @Dai I understand I can take the string apart and do what I want with it...manually. What you suggest is not in "MMM dd, yyyy" format, which was the entire point of this - letting date-fns do that work for me. – Tsar Bomba Nov 18 '22 at 00:52
  • @Dai Did you read the original post? – Tsar Bomba Nov 18 '22 at 01:00
  • ...not close enough, erk. I just realised how much time of yours I wasted barking up the wrong tree. _sorry_ – Dai Nov 18 '22 at 01:01

0 Answers0