I'm trying to get a jquery or java-script code for displaying the current month and year in a div, but unable so far. I mean I want to display the current month and year in this format:October 2012
so that every month I don't need to edit it or anything.
I saw many questions here, but none shows how to display the variable in a div.
Any idea how to accomplish that?
Your help and ideas are much appreciated
JavaScript doesn't natively implement strftime
, so you'll have to do something a bit less elegant:
window.onload = function() {
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];;
var date = new Date();
document.getElementById('date').innerHTML = months[date.getMonth()] + ' ' + date.getFullYear();
};
I'm assuming that you have an element with an id
of date
somewhere in your HTML.
there's no inbuild function in javascript to get full name of month. You can create an array of month names and use it to get full month name for e.g
var m_names = ['January', 'February', 'March',
'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
d = new Date();
var n = m_names[d.getMonth()];
Get current Date, month, year or ETC....
new Date().getDate() // Get the day as a number (1-31)
new Date().getDay() // Get the weekday as a number (0-6)
new Date().getFullYear() // Get the four digit year (yyyy)
new Date().getHours() // Get the hour (0-23)
new Date().getMilliseconds() // Get the milliseconds (0-999)
new Date().getMinutes() // Get the minutes (0-59)
new Date().getMonth() // Get the month (0-11)
new Date().getSeconds() // Get the seconds (0-59)
new Date().getTime() // Get the time (milliseconds since January 1, 1970)
Look at moment() http://momentjs.com
moment().format('MMMM YYYY');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With