I have this code which shows the year in YYYYMMDD format. I would like the output to be in YYMMDD
format. Basically to replace 2014 to 14 only.
let date = new Date();
let timeNow = new Date(date.valueOf()-60*1000*date.getTimezoneOffset()).toISOString().substring(0, 10).replace(/-/g,"");
console.log(timeNow);
PS: I'm in a +5:30 timezone if that matters. My initial code was without the whistles but someone commented that I have to use getTimezoneOffset else there may be a difference of 5.5 hours and create problems in the future:
let date = new Date();
let timeToday = new Date(Date.now()).toISOString().slice(0, 10).replace(/-/g,"");
console.log(timeToday);
To convert the date format from YYYYMMDD
to YYMMDD
, you can modify your code to extract the last two digits of the year.
let date = new Date();
let timeNow = new Date(date.valueOf() - 60 * 1000 * date.getTimezoneOffset())
.toISOString()
.substring(0, 10)
.replace(/-/g, "")
.substring(2); // Extract the last two digits of the year
console.log(timeNow);
The substring(2)
method removes the first two characters of the string, effectively converting YYYYMMDD
to YYMMDD
.