Convert a date to string in Javascript
Asked Answered
R

6

8

I'm looking for a way to convert a Javascript Date object to a string. I'm converting my site from Ruby to server side Javascript, and I'm looking for something analogous to strftime in Ruby, C, and many other languages.

I found plenty of simple scripts that do this kind of conversion, but I'd prefer not to include a custom implementation if there is a standard way of doing this.

I'm not using a Javascript framework. I'm using Mozilla Rhino, but would prefer to stay away from using the Java library as much as possible, to allow moving my code easily between implementations.

I want to be able to specify the format of the string, since I want to embed it in a sentence. I want to be able to insert arbitrary on's and at's, and have the full name of the day, not just it's abbreviation. So toString() won't suffice.

Remainderman answered 19/12, 2009 at 16:21 Comment(1)
There is also pretty date (ejohn.org/blog/javascript-pretty-date)Oni
V
2

MomentJS has a very robust set of time formatting options, and has also been updated recently.

Verine answered 18/9, 2012 at 14:21 Comment(0)
C
9

There is a free (and awesome) library for Javascript called Datejs that gives you all sorts of formatting and parsing capabilities (see the FormatSpecifiers documentation).

Chesson answered 19/12, 2009 at 16:27 Comment(4)
While I prefer not to use an external library, this is exactly what I need. Sad however that they label it as alpha, and not update it for 2+ years.Remainderman
I hear you, but you could just download the latest and use the code as a reference to build your own. They have tests, too (datejs.com/test). It's a good library that alot of people use.Chesson
Even better, start updating it yourself. You’ll have an awesome JS date library, and by improving it and making your improvements available to the rest of us, we’ll have an easier time of things, and you’ll have Internet Fame. You can’t put a price on Internet Fame.Etka
While the library is very nice, I decided not to use it for this specific case since it has way too many features for what I need. I am going to use it in the future for other projects, though.Remainderman
E
6

There isn’t anything built into JavaScript that allows you to format the display of a date like strftime does.

The closest is Date().toLocaleString(), which returns the date as a string formatted according to the local time zone.

Etka answered 19/12, 2009 at 16:28 Comment(0)
P
2

Dates have a built-in toString:

alert(new Date().toString())

"Sat Dec 19 2009 08:23:33 GMT-0800 (Pacific Standard Time)"

Paramorphism answered 19/12, 2009 at 16:23 Comment(0)
V
2

MomentJS has a very robust set of time formatting options, and has also been updated recently.

Verine answered 18/9, 2012 at 14:21 Comment(0)
S
1

You could use

new Date().toString()

...to get a localized output of the date.

Subminiature answered 19/12, 2009 at 16:23 Comment(3)
I should have added, that I want to be able to specify the format of the string, since I want to embed it in a sentence. I want to be able to insert arbitrary on's and at's, and have the full name of the day, not just it's abbreviation.Remainderman
Why so many people vote this up? There's nothing flexible about this. He said he needs something analogous.Chesson
No it was part of his original questionChesson
H
1

You could use my JavaScript implementation of Java's SimpleDateFormat: http://www.timdown.co.uk/code/simpledateformat.php. It's a little lighter than date.js and is kept up to date as it is included in log4javascript.

Hobo answered 20/12, 2009 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.