DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Javascript's lack of strftime

Javascript's lack of strftime

Swizec Teller user avatar by
Swizec Teller
·
Dec. 15, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
2.88K Views

Join the DZone community and get the full member experience.

Join For Free

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...

image via wikipedia

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 …

from http://swizec.com/blog/javascripts-lack-of-strftime/swizec/3164

JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OpenTelemetry in Action: Identifying Database Dependencies
  • Role of Development Team in an Agile Environment
  • Enough Already With ‘Event Streaming’
  • Choosing Between REST and GraphQL

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo