When that date is converted to local time a date shift may occur Seems to me that when Date constructor receives a string that matches first part of ISO 8601 format (date part) it does a precise date conversion in UTC time zone with 0 time. Trying to add my 2 cents to this thread (elaborating on answer). If you continue with negative numbers you can continue rolling back another day new Date(2011, "02", -1) new Date(2011, "02", 0) // Again the zero roles back from March to the last day of February.Īdding a day of zero to any year and month argument will get the last day of the previous month. If we do the same thing but with a day of zero, we get something different. Note the month is March because zero based months again. Get the last month and day of a year new Date((2011 + 1), 0, 0) // The second zero roles back one day into the previous month's last day.Įxample of Number, String arguments. Get the first month and day of a given year new Date(2011, 0) // Normal behavior as months in this case are zero based. Note: arguments can be of type Number or String. If you provide separate arguments to the Date constructor you can get other useful outputs as described below I typically have to manage the date format T00:00:00 so this is what I do. Now change hyphen to forward slash as before what happens? new Date("4T00:00:00") We could easily change hyphens in your date "" when making a new date new Date("".replace(/-/g, '\/')) // => "4". However, if we rearrange the string format to Month-Day-Year. Note: The following examples may or may not be ONE DAY OFF depending on YOUR timezone and current time. There are several crazy things that happen with a JS DATE object that convert strings, for example consider the following date you provided
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |