Hope and appreciate to get some explanation for the following
The problem is about dates, and I know I could use a package like moments, though, in this situation I don’t need to use dates that much. I also know that day and month obtained from respective Date get functions return a value which is one less then the actual corresponding value.
So, I have a form that saves data to a database and as for displaying saved data I need to convert dates to the format yyyy-mm-dd and for that I use a small function and the approach of separating date parts and rejoin them in proper order for the date picker.
The function used to format the dates (sorry for all the logs)
This function is called twice for each record, because there are two dates per record, and the console output is the following:
// the code for the function
export function dateToPug(dateIso) {
console.log('ndate string from db' , dateIso, 'n')
const date = new Date(dateIso)
console.log('toDateString',date.toDateString(), 'n')
console.log('date string converted to date', date.toISOString(), 'n')
const day = (date.getDate())// + 1)
console.log('day extracted from date', day, 'n')
// day < 10 ? day =`0${date.getDate()}` : ''
const month = (date.getMonth())// + 1)
console.log('month extracted from date', month, 'n')
// month < 10 ? `0${date.getMonth()}` : ''
const year = date.getFullYear()
console.log(`${year}-${month}-${day}`, 'n')
return `${year}-${month}-${day}`
}
// the outupt
date string from db **1900**-**10**-**10**T00:00:00.000Z // first date from the database record
toDateString Tue **Oct** **09** 1900 // converted to date string changes the day but not the month
date string converted to date **1900-10-10**T00:00:00.000Z // date in iso format
day extracted from date **9** // got day -1 as expected
month extracted from date **9** // got month -1 as expected
**1900-9-9** // date in format for the form input date (with trimmings that will have to be removed)
date string from db **1950**-**10**-**10**T00:00:00.000Z // second date from the database record
toDateString Tue **Oct** **10** 1950 // converted to date string neither change the day nor the month
date string converted to date **1950-10-10**T00:00:00.000Z // date in iso format
day extracted from date **10** // got day unchanged
month extracted from date **9** // got month -1 as expected
**1950-9-10** // date in format for the form input date (with trimmings that will have to be removed)
What am I missing?
2
Answers
That is how I managed to solve it, at least for know. On a later stage I eventually use a Library. But not for now.
You are not accounting for timezone differences.
I added this line to your function
and tested with node.js:
getDate()
returns 1-31, i.e. the actual day of the month, whilegetMonth()
returns 0-11, i.e. month – 1, so this is correctly indicating month 9 day 9 for the noted timezone offset.Note that 12:00 am on Oct 10 in London(UTC) is still Oct 9 for the entire western hemisphere.
The date functions default to using the local system timezone. Depending on how you are running the code, you may need to explicitly specify a timezone setting to get that to behave the way you want.
Or just leverage the years of work by other people who have already worked on this problem, and use a datetime library.