Loading ...
Sorry, an error occurred while loading the content.

JavaScript's Grip of the Day: Formatting Dates

Expand Messages
  • Shlomi Fish
    As I was working on a new Greasemonkey script today, I needed a way to parse the date from a string, and format it into DD-MM-YYYY. Parsing the date turned out
    Message 1 of 1 , Nov 25, 2008
    • 0 Attachment
      As I was working on a new Greasemonkey script today, I needed a way to parse
      the date from a string, and format it into DD-MM-YYYY. Parsing the date
      turned out to be easy:

      https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Global_Objects:Date#Methods

      However, now I wanted to find a way to format it. I landed on this post:

      http://blog.stevenlevithan.com/archives/date-time-format

      As it turns out, JavaScript has no built-in method equivalent to strftime(),
      and this post supplies an alternative. So I thought to myself: no problem -
      I'll just use sprintf. But, lo and behold - no sprintf for us:

      http://www.google.com/search?q=javascript%20sprintf

      So I bit the bullet and copy-and-pasted the date-time-format code into my
      script. The format supported by it is not strftime()-compatible, but rather
      something like ColdFusion. And the script now works.

      But I'm really surprised that JavaScript doesn't even have something like
      sprintf. That's really lame.

      Regards,

      Shlomi Fish

      --
      -----------------------------------------------------------------
      Shlomi Fish http://www.shlomifish.org/
      "Humanity" - Parody of Modern Life - http://xrl.us/bkeut

      Shlomi, so what are you working on? Working on a new wiki about unit testing
      fortunes in freecell? -- Ran Eilam
    Your message has been successfully submitted and would be delivered to recipients shortly.