I don't know much about Javascript, and the other questions I found are related to operations on dates, not only getting the information as I need it.
Objective
I wish to get the date as below-formatted:
Printed on Thursday, 27 January 2011 at 17:42:21
So far, I got the following:
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
var prnDt = "Printed on Thursday, " + now.getDate() + " January " + now.getFullYear() + " at " + h + ":" + m + ":" s;
I now need to know how to get the day of week and the month of year (their names).
Is there a simple way to make it, or shall I consider using arrays where I would simply index to the right value using now.getMonth()
and now.getDay()
?
As @L-Ray has already suggested, you can look into moment.js as well
Sample
Using http://phrogz.net/JS/FormatDateTime_JS.txt you can just:
Unfortunately,
Date
object in javascript returns information about months only in numeric format. The faster thing you can do is to create an array of months (they are not supposed to change frequently!) and create a function which returns the name based on the number.Something like this:
Your code therefore becomes:
That's simple. You can set option to display only week days in toLocaleDateString() to get the names. For example:
(new Date()).toLocaleDateString('en-US',{ weekday: 'long'}) will return only the day of the week. And (new Date()).toLocaleDateString('en-US',{ month: 'long'}) will return only the month of the year.
You can look at datejs which parses the localized date output for example.
The formatting may look like this, in your example: