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);
2
Answers
To convert the date format from
YYYYMMDD
toYYMMDD
, you can modify your code to extract the last two digits of the year.The
substring(2)
method removes the first two characters of the string, effectively convertingYYYYMMDD
toYYMMDD
.Try this one:
let date = new Date();
let timeNow = new Date(date.valueOf() – 60 * 1000 * date.getTimezoneOffset()).toISOString().slice(2, 10).replace(/-/g, "");
console.log(timeNow);