You know that one piece of shitty code that always makes you cringe? Something along the lines of months = ['Jan', 'Feb' ....]; dateString = date.day()+' '+months[date.month()];
Yeah that piece of code. Let's talk about that.
A couple of days ago I caught myself writing code like that and red flags and alarms and air-raid sirens started going off in my head. I felt like that proverbial axe murderer reading my code who knows where I live was already breathing down my neck.
Scary situation that.
// all of this correctly returns a millisecond timestamp since unix epoch// the string argument can also be passed to the date constructor by the way (returns a correct Date object)Date.parse("Aug 9, 1995");Date.parse("Wed, 09 Aug 1995 00:00:00 GMT");Date.parse("Wed, 09 Aug 1995 00:00:00");Date.parse("Thu, 01 Jan 1970 00:00:00 GMT");Date.parse("Thu, 01 Jan 1970 00:00:00");Date.parse("Thu, 01 Jan 1970 00:00:00 GMT-0400");
Have a date and want a string? You're shit out of luck son.
> var d = new Date()> dMon, 12 Dec 2011 11:28:55 GMT> d.toString()'Mon Dec 12 2011 12:28:55 GMT+0100 (CET)'> d.toDateString()'Mon Dec 12 2011'> d.toISOString()'2011-12-12T11:28:55.401Z'> d.toJSON()'2011-12-12T11:28:55.401Z'> d.toGMTString()'Mon, 12 Dec 2011 11:28:55 GMT'> d.toLocaleDateString()'Monday, December 12, 2011'> d.toLocaleString()'Mon Dec 12 2011 12:28:55 GMT+0100 (CET)'> d.toLocaleTimeString()'12:28:55'> d.toString()'Mon Dec 12 2011 12:28:55 GMT+0100 (CET)'> d.toTimeString()'12:28:55 GMT+0100 (CET)'> d.toUTCString()'Mon, 12 Dec 2011 11:28:55 GMT'
Notice how none of those functions accept a format parameter? Sure, you could get those strings and hack them apart to create a string formatted to your liking ... but I'm not sure that's the ideal approach.
And let's not even get into wanting to coerce a 12-hour time format out of this thing. Perhaps if your computer is situated in the US the localeString would return that?
However I was able to find a jquery plugin that implements a rough strftime function, although it doesn't seem to have been actively worked on for the past three years and there are many things missing.
Here's how it works 👇
Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.
"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Ready to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization
Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.
Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️