how do I get an iso formated local date that matches my OS's one in js?

@corn flour you know...
21 Replies
venego
venego16mo ago
so Date().toISOString() is utc. and I'm in utc+1 I need to add an hour. how do I do so, It's fine for me to manipulate dates to get the right one for me but I don't wanna manipulate strings
cornflour
cornflour16mo ago
just as confirmation so you want the format yyyy-mm-ddThh:mm:ss in local time right?
venego
venego16mo ago
I actually want a space instead of T. but it's a problem. so yes. that'll work
cornflour
cornflour16mo ago
you got pretty close, just that month needs to +1 because month start at 0 and if you are not completely attached to yyyy-mm-dd, maybe look into Intl.DateTimeFormat (theres also formateToParts which can return an array of the parts of the string instead)
venego
venego16mo ago
I guess Intl could be my only option besides using linux posix tools I'll try Intl. the first example on my machine is not shifted by month but 2 days forwards. which is really weird.
cornflour
cornflour16mo ago
can you show me the code?
venego
venego16mo ago
const date = new Date();
const anotherDate = new Date(
date.getFullYear(),
date.getMonth(),
date.getDay(),
date.getHours(),
date.getMinutes(),
date.getSeconds()
);
console.log(anotherDate)
// output: 3 days in the future then -1 an hour

console.log(Intl.DateTimeFormat(undefined, {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
}).format(date))
// output: accurate
const date = new Date();
const anotherDate = new Date(
date.getFullYear(),
date.getMonth(),
date.getDay(),
date.getHours(),
date.getMinutes(),
date.getSeconds()
);
console.log(anotherDate)
// output: 3 days in the future then -1 an hour

console.log(Intl.DateTimeFormat(undefined, {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
}).format(date))
// output: accurate
can I get an absolute date(without the PM/AM) with Intl
venego
venego16mo ago
venego
venego16mo ago
oh I can.
venego
venego16mo ago
worked
venego
venego16mo ago
I just have to deal with the weird m/d/y
cornflour
cornflour16mo ago
you can yes and the first one should be getDate, not getDay (which returns the day of the week) im not sure about the -1 hour since i cant replicate it
venego
venego16mo ago
unless it's behind by two months and and two days forwards???? idk, this is confusing oh getDate worked thanks. I'll stick to the first one and add an hour
cornflour
cornflour16mo ago
just out of curiosity, does this have 1 hour off to you?
const formatDate = (d) => {
const year = d.getFullYear()
const month = d.getMonth() + 1
const date = d.getDate()
const h = d.getHours()
const m = d.getMinutes()
const s = d.getSeconds()
return `${year}-${month}-${date} ${h}:${m}:${s}`
}

console.log(formatDate(new Date()))
const formatDate = (d) => {
const year = d.getFullYear()
const month = d.getMonth() + 1
const date = d.getDate()
const h = d.getHours()
const m = d.getMinutes()
const s = d.getSeconds()
return `${year}-${month}-${date} ${h}:${m}:${s}`
}

console.log(formatDate(new Date()))
venego
venego16mo ago
no it's accurate. hmm interesting and why the date is behind by month(before you add 1)?
cornflour
cornflour16mo ago
because getMonth returns month starting at 0 for whatever reason
venego
venego16mo ago
why are these two different in the hour?
// hours in 0 utc
const accDate = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours() + 1,
date.getMinutes(),
date.getSeconds()
);

// hours match local time
const formatDate = (d) => {
const year = d.getFullYear();
const month = d.getMonth() + 1;
const date = d.getDate();
const h = d.getHours();
const m = d.getMinutes();
const s = d.getSeconds();
return `${year}-${month}-${date} ${h}:${m}:${s}`;
};
// hours in 0 utc
const accDate = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours() + 1,
date.getMinutes(),
date.getSeconds()
);

// hours match local time
const formatDate = (d) => {
const year = d.getFullYear();
const month = d.getMonth() + 1;
const date = d.getDate();
const h = d.getHours();
const m = d.getMinutes();
const s = d.getSeconds();
return `${year}-${month}-${date} ${h}:${m}:${s}`;
};
maybe because 30 and 31 difference in months????
cornflour
cornflour16mo ago
i have no clue what you mean the new Date i created works fine with my local time
venego
venego16mo ago
maybe you're utc 0. I'm in +1.
cornflour
cornflour16mo ago
nah im in PST
venego
venego16mo ago
i'ts just a coincidence in your case. oh??? that is weird