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

@corn flour you know...
21 Replies
venego
venego2y 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
cornflour2y ago
just as confirmation so you want the format yyyy-mm-ddThh:mm:ss in local time right?
venego
venego2y ago
I actually want a space instead of T. but it's a problem. so yes. that'll work
cornflour
cornflour2y 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
venego2y 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
cornflour2y ago
can you show me the code?
venego
venego2y 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
venego2y ago
venego
venego2y ago
oh I can.
venego
venego2y ago
worked
venego
venego2y ago
I just have to deal with the weird m/d/y
cornflour
cornflour2y 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
venego2y 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
cornflour2y 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
venego2y ago
no it's accurate. hmm interesting and why the date is behind by month(before you add 1)?
cornflour
cornflour2y ago
because getMonth returns month starting at 0 for whatever reason
venego
venego2y 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
cornflour2y ago
i have no clue what you mean the new Date i created works fine with my local time
venego
venego2y ago
maybe you're utc 0. I'm in +1.
cornflour
cornflour2y ago
nah im in PST
venego
venego2y ago
i'ts just a coincidence in your case. oh??? that is weird
Want results from more Discord servers?
Add your server