Swizec Teller - a geek with a hatswizec.com

    Javascript's lack of strftime

    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()];

    An air-raid siren in Nice, France, is still op...

    Yeah that piece of code. Let's talk about that.

    It sucks. There is a special circle of hell for people who do it and yet JavaScript developers are forced to doing it all the freaking time!

    In JavaScript that's the only way you can do it. I shit you not.

    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.

    But when I went looking for an elegant way of coercing datetime into a string, I found the whole situation rather lacking. Turns out JavaScript doesn't have a native strftime function even though it is surprisingly brilliant in converting strings to native representations.

    String -> Date

    Have a string? Want a date? JavaScript's got your back bro!

    // 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");

    Date -> String

    Have a date and want a string? You're shit out of luck son.

    Pretty much the only date to string conversions javascript natively supports is returning a locale string, which is nonstandard and varies greatly computer to computer, and forms of standardized ISO time. It can do this and only this in numerous ways even!

    > var d = new Date()
    > d
    Mon, 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.

    What?

    So there you have it. JavaScript's epic and yet abysmally poor support for date->string conversions.

    What do you think is it about the javascript ecosystem that tolerates this? Countless developers must have been implementing shitty code because of this and nobody has thought to complain? Nobody has gone so far as to suggest adding a simple strftime function to the language? Most popular languages seem to have this in their standard library ...

    Enhanced by Zemanta

    Did you enjoy this article?

    Published on December 12th, 2011 in ISO 8601, JavaScript, Languages, Programming, Tools, Uncategorized,

    Learned something new?
    Read more Software Engineering Lessons from Production

    I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.

    Subscribe below 👇

    Software Engineering Lessons from Production

    Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open 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. 👌"

    ~ Ashish Kumar

    Join 15,883+ engineers learning lessons from my "raw and honest from the heart" emails.

    ⭐️⭐️⭐️⭐️✨
    4.5 stars average rating

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

    Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Want to brush up on modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️