javascript - toLocaleDateString() is subtracting a day -
i'm pulling dates sql database treats them dates start @ midnight. when go use tolocaledatestring() on them, formats them properly, not before losing day.
before formatting: 2011-09-01t00:00:00
after formatting: 8/31/2011
code:
plan.datereceived = new date(plan.datereceived).tolocaledatestring()+','+plan.datereceived; why this, , inline fix can make have behave properly? found another post had similar problem, i'm not 100% convinced it's timezone issue.
if run code in pieces, you'll notice new date('2011-09-01t00:00:00') produces output wed aug 31 2011 20:00:00 gmt-0400 (edt) (my computer in edt right now).
this because (doc):
differences in assumed time zone
given date string of "march 7, 2014", parse() assumes local time zone, given iso format such "2014-03-07" assume time zone of utc. therefore date objects produced using strings represent different moments in time unless system set local time zone of utc. means 2 date strings appear equivalent may result in 2 different values depending on format of string being converted (this behavior changed in ecmascript ed 6 both treated local).
converting locale date string convert string appropriate browser's locale. documentation indicates "the default runtime's default time zone".
if want ensure string in utc time, use
new date('2011-09-01t00:00:00').tolocaledatestring('en-us', {timezone: 'utc'})
Comments
Post a Comment