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.
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()
?
Yes, you'll need arrays.
const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
const months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var day = days[ now.getDay() ];
var month = months[ now.getMonth() ];
Or you can use the date.js library.
EDIT:
If you're going to use these frequently, you may want to extend Date.prototype
for accessibility.
(function() {
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
Date.prototype.getMonthName = function() {
return months[ this.getMonth() ];
};
Date.prototype.getDayName = function() {
return days[ this.getDay() ];
};
})();
var now = new Date();
var day = now.getDayName();
var month = now.getMonthName();