In my research to answer this question, I did note that the .toISOString method (MDN HERE) has most of what you are looking for without the odd complexity of my first answer. Since the output pattern is fixed, you might be able to slice out the parts you want, get the Day of Week and reassemble it all quickly.
Generating something like that is outside the patterns used in the article. The date / time patterns used are for local-formatted dates and times.
Unfortunately, the date / time pattern you have would have to be generated via string formatting ... so, you's end up with something ugly like (only doing partial pattern) ...
vard=newDate();varcurr_date=d.getDate();varcurr_month=d.getMonth()+1;//Months are zero basedvarcurr_year=d.getFullYear();console.log(`${curr_year}-${curr_month}-${curr_date}`);
... this is why tools such as moment.js are so popular. Although, saying this, I would look around at the various date / time tools. There have been several new ones and you might find a tool that does what you want with a smaller footprint.
Thanks for looking into that Bob.
I figured since you're the date/time guru, you'd have a smoother answer than the func I use, which I wrote a while back. ;)
/**
@func util
add a leading zero, if the number is a single digit number
i.e. prefix with a zero, if under 10
@param {number} t - a time or date chunk, representing either the month, day-of-month, hour, minute, or second
@return {string}
*/constadd0=t=>t<10?`0${t}`:String(t);
/**
@func
get the current time, in a canonical human-readable format:
"YYYY-MM-DD ddd HH:mm"
@cons
no seconds
no milliseconds
@return {string}
*/exportconstgetDateStandard=()=>{constdt=newDate();consty=dt.getFullYear();constm=add0(dt.getMonth()+1);constd=add0(dt.getDate());//day of monthconstw=dt.toDateString().substring(0,3);//day of week enum, either Mon, Tue, Wed, Thu, Fri, Sat, Sunconsth=add0(dt.getHours());constmin=add0(dt.getMinutes());return`[${y}-${m}-${d}${w}${h}:${min}]`;};
Your solution is probably the clearest pattern. I was thinking about the toISOString and having an ability to pull out whole patterns, the problem is that is doesn't take into account the timezone offset.
Nice tips Bob.
What would be your approach to getting this date format?...
In my research to answer this question, I did note that the
.toISOString
method (MDN HERE) has most of what you are looking for without the odd complexity of my first answer. Since the output pattern is fixed, you might be able to slice out the parts you want, get the Day of Week and reassemble it all quickly.Generating something like that is outside the patterns used in the article. The date / time patterns used are for local-formatted dates and times.
Unfortunately, the date / time pattern you have would have to be generated via string formatting ... so, you's end up with something ugly like (only doing partial pattern) ...
... this is why tools such as moment.js are so popular. Although, saying this, I would look around at the various date / time tools. There have been several new ones and you might find a tool that does what you want with a smaller footprint.
Thanks for looking into that Bob.
I figured since you're the date/time guru, you'd have a smoother answer than the func I use, which I wrote a while back. ;)
Your solution is probably the clearest pattern. I was thinking about the
toISOString
and having an ability to pull out whole patterns, the problem is that is doesn't take into account the timezone offset.Maybe something like this ...
That's a great one if one wants to timestamp in UTC time, or another timezone.
Thanks!