Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MatthewLTL

macrumors 68000
Original poster
Jan 22, 2015
1,684
18
Rochester, MN
I am trying to get a proper code for my website. there is this code:
Code:
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
</script>
which will display the date like this: Tue Mar 25 2015.

I want the date to output EXACTLY like this:
Code:
Tuesday, March 25, 2015
all i need to know is that code (or modification to the above code) to make it display the way i want.
 
Last edited by a moderator:

D.T.

macrumors G4
Sep 15, 2011
11,050
12,460
Vilano Beach, FL
You're going to need to either:

1) Access the various date object properties and build the date format you'd like, i.e., date.getDay() + " " + date.getDate() [etc]

2) Use an additional library. I use Moment.js for all my JS date/time formatting (both server and client side).

JS doesn't have an inherent formatter like some other langs (ex: new Date("mm-dd-yyyy") )
 
Last edited:

D.T.

macrumors G4
Sep 15, 2011
11,050
12,460
Vilano Beach, FL
Yup, in PHP it's as simple as $date = date('l, F j, Y').

It's pretty common in most langs/frameworks, PHP like your example, Ruby/Rails has the handy date.to_formatted_s that takes a formatting symbol, C#/.NET with the handy date.ToString() with standard-ish formatting strings.

It's kind of a silly omission on the part of Javascript, and adding a library is pretty trivial (though it does create kind of a silly dependency).

I've recently been converting (to get my feet wet) some of a Web API currently in Nodejs to Go, and the latter has brilliant date formatting, you use a mnemonic to express a date (and/or time), so no cryptic codes (that after 20-something years I still forget ...)

:cool:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.