-1

How can I convert this date: 29/12/2022 where: 29 is day, 12 is month, 2022 is year, to ISO string.

I tried this:

var dateStr = "29/12/2022";
var parts = dateStr.split("/")
var myDate = new Date(parseInt(parts[2]), parseInt(parts[1]) - 1, parseInt(parts[0]));
console.log(myDate.toISOString());
// 2024-05-11T22:00:00.000Z this is wrong

I was expecting different result.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
makaMa
  • 11
  • 3
  • *"12 is day, 29 is month, 2022 is year"* ??? – adiga Sep 12 '22 at 15:47
  • Sorry, my mistake. I fixed my question – makaMa Sep 12 '22 at 15:49
  • I get -> `2022-12-29T00:00:00.000Z`, did fixing your question, also answer it? – Keith Sep 12 '22 at 15:52
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString – Scott Marcus Sep 12 '22 at 15:56
  • Are the day and month always 0 padded? If so just use substring functions – Salman A Sep 12 '22 at 16:04
  • Your approach is acceptable but use new Date(Date.UTC(..., ...)) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC – Salman A Sep 12 '22 at 16:08
  • @SalmanA It's not really clear from the OP that he want's it parsing as UTC, he just wanted to show the current date time as an ISO string, if you parse the current time as UTC you could possibly get a different time, and even date. – Keith Sep 12 '22 at 16:32
  • 1
    @Keith my comments were based on the assumption that op wants 29/12/2022 to become 2022-12-29T00:00:00Z – Salman A Sep 12 '22 at 18:14
  • @SalmanA He maybe does, but `Date(y,m,d)` is a locale date constructor. But yeah, maybe the OP didn't know this. Unfortunately this detail is missing from the question.. :( – Keith Sep 13 '22 at 10:05

3 Answers3

0

There is no need to parseInt and to remove 1 to the month.

var dateStr = "29/12/2022";
var parts = dateStr.split("/")
var myDate = new Date(`${parts[2]}-${parts[1]}-${parts[0]}`);
console.log(myDate.toISOString());
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • `YYYY-MM-DD` is actually not a standard, it will likely work, but to be sure it might be best making into a valid ISO string by adding `T00:00:00`, or keep as the op and use multiple params,.. `Date(parts[2],parts[1]-1,parts[0]`); – Keith Sep 12 '22 at 16:00
  • 1
    `YYYY-MM-DD` is ok as per [ECMA262](https://tc39.es/ecma262/#sec-date-time-string-format) – Louys Patrice Bessette Sep 12 '22 at 16:18
  • 1
    I was assuming the OP wanted to keep things locale. From MDN it says -> Date-only strings (e.g. "1970-01-01") are treated as UTC, while date-time strings (e.g. "1970-01-01T12:00") are treated as local. But if the OP is wanting UTC time then this should be ok. – Keith Sep 12 '22 at 16:24
0

In my code usually I do something like this:

const dateStr = "29/12/2022";
const parts = dateStr.split("/");
const date = new Date(0); // It will set hours, minutes, and seconds to 0
date.setDate(parts[0]);
date.setMonth(parts[1]-1);
date.setFullYear(parts[2]);
console.log(date.toISOString());
0

It may not be the most optimal way but you could do it this way as long as the month when it is a single digit is sent with zero at the beginning type: 01 -> January

let date = '29/12/2022';
let dateFormat = date[3]+date[4]+"-"+date[0]+date[1]+"- 
"+date[6]+date[7]+date[8]+date[9]
// 12-29-2022
let mydate = new Date(dateFormat)
// Thu Dec 29 2022 00:00:00 GMT-0500 
Nomezquy
  • 1
  • 1
  • Passing a string "year-month-day" to the constructor is not standard and browser-dependent. – Christian Vincenzo Traina Sep 12 '22 at 16:07
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 16 '22 at 11:24