I need to add the current time to a date, but there is no way to do it
const d = new Date();
const time = {
seconds: d.getSeconds(),
minuts: d.getMinutes(),
hours: d.getHours(),
mills: d.getMilliseconds()
}
const dd = new Date("2022-11-21");
let newDate = `${dd}T${time.hours}:${time.minuts}:${time.seconds}.${time.mills}Z`
let ff = new Date(newDate)
console.log(d.toISOString());
console.log(ff.getUTCDate());
dd
makes no sense. When using it in a template literal, you’re coercing theDate
object to a string, butString(new Date("2022-11-21"))
is a localized string of the full date; however, for whatever reason, you assume that it’s part of an ISO 8601 string. Why? Read the documentation. You probably meant${dd.toISOString().split("T")[0]}
.Date
object has a bunch of constructor overloads that you can use to pass each part of the date in individually. You probably want something likenew Date(dd.getYear(), dd.getMonth(), dd.getDay(), time.hours, time.minuts, time.seconds, time.mills)
.