Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display current month, year in javascript using div

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

like image 985
Digital site Avatar asked Oct 06 '12 07:10

Digital site


4 Answers

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.

like image 112
Blender Avatar answered Oct 26 '22 13:10

Blender


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()]; 
like image 39
FosterZ Avatar answered Oct 26 '22 15:10

FosterZ


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)
like image 45
Parth Jasani Avatar answered Oct 26 '22 13:10

Parth Jasani


Look at moment() http://momentjs.com

moment().format('MMMM YYYY');
like image 42
chovy Avatar answered Oct 26 '22 13:10

chovy