0

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());
j08691
  • 204,283
  • 31
  • 260
  • 272
  • The way you use `dd` makes no sense. When using it in a template literal, you’re coercing the `Date` object to a string, but `String(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](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date). You probably meant `${dd.toISOString().split("T")[0]}`. – Sebastian Simon Nov 23 '22 at 20:49
  • 1
    The `Date` object has a bunch of [constructor overloads](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date) that you can use to pass each part of the date in individually. You probably want something like `new Date(dd.getYear(), dd.getMonth(), dd.getDay(), time.hours, time.minuts, time.seconds, time.mills)`. – Jesse Nov 23 '22 at 20:53

1 Answers1

1

If you want the current time to a past date, you should just grab the time from d and stick it on dd

const d = new Date();

const dd = new Date("2022-11-21");
dd.setSeconds(d.getSeconds())
dd.setMinutes(d.getMinutes())
dd.setHours(d.getHours())
dd.setMilliseconds(d.getMilliseconds())

console.log(dd)
  • https://www.typescriptlang.org/play?strict=false#code/MYewdgzgLgBAJjAvDMBTA7jAIgQyqgCgEoBuAKFEligEsBbVJGAbzJhglUrggC54AdAHNUUAMpdwPYgBo2MOjTABXKH0EioAWSWrUEWfIAWIZQCd1cYaIASpi4faKANs8vXtNVzU7cDRMgBfMgpwaHgEZDRMXHwCACIAJgAGRMSAWgBGTPTEzPjSMjgrTnFJMGlaBgFfKQgA4prRHRV8AyrUAUVW+qKS23t2+k6Tc17G0p1vWoqh6pc3AMoIEGdO5xAhAmKiIA – Giacomo Gagliano Nov 23 '22 at 20:58
  • 1
    When I downvoted you didn't have a code example in your answer, it was just an extremely vague description. Next time add an example before you post the answer. – Jesse Nov 23 '22 at 21:00
  • yeah sorry ... i posted a shorter version now .. you were creating a useless pivot object – Giacomo Gagliano Nov 23 '22 at 21:02
  • Does it work for you? I would appreciate an upvote ahaha I am missing 7 points reputation to write comments ... actually I wanted to comment on your post, but I am not yet allowed! please help me ehehe – Giacomo Gagliano Nov 23 '22 at 21:03
  • @Jesse ohh sorry, I thought you were the creator of the post ... I am still new on here .. even tho i subscribed one year and half ago! – Giacomo Gagliano Nov 23 '22 at 21:07
  • All good. Also, there is some weird behavior with the `yyyy-mm-dd` date format. It treats it as ISO 8601 format which assumes the date is GMT if not specified, so it may produce some unexpected results. I would recommend using a different format. Simply replacing the dashes with forward slashes should make it use the local time zone. – Jesse Nov 23 '22 at 21:18