>>> Programming >> Javascript > How to get the date with Javascript? (This page has been seen 824 times)
How to get the date with Javascript?
Here is some examples on how to work with dates with Javascript.
I first build the javascript and then i write it into the div element called date
First of notice that i have made two functions, one called returnDay for returning the name of the day, and another called returnMonth for returning the name of the Month.. Also notice that in Javascript the Month January is 0, and the Month December is 11. Not 1 and 12. The same to notice is the Day, and javascript starts a week with Sunday, and Sunday is 0, and Saturday is 6.
Another thing to notice is that Javascript has no way of returning the month with a 0 in front of it. So you need to do a ? operator, or an if to do this, its up to you. I have done it like this. (currentMonth+1<10 ? '0'+(currentMonth+1) : currentMonth+1) This means that if the month is below 10 then it inserts a 0 infront of the digit.
The Code:
I first build the javascript and then i write it into the div element called date
First of notice that i have made two functions, one called returnDay for returning the name of the day, and another called returnMonth for returning the name of the Month.. Also notice that in Javascript the Month January is 0, and the Month December is 11. Not 1 and 12. The same to notice is the Day, and javascript starts a week with Sunday, and Sunday is 0, and Saturday is 6.
Another thing to notice is that Javascript has no way of returning the month with a 0 in front of it. So you need to do a ? operator, or an if to do this, its up to you. I have done it like this. (currentMonth+1<10 ? '0'+(currentMonth+1) : currentMonth+1) This means that if the month is below 10 then it inserts a 0 infront of the digit.
The Code:
function returnDay(day){
if(day==0) return 'Sunday';
if(day==1) return 'Monday';
if(day==2) return 'Tuesday';
if(day==3) return 'Wednesday';
if(day==4) return 'Thursday';
if(day==5) return 'Friday';
if(day==6) return 'Saturday';
}
function returnMonth(month){
if(month==0) return 'January';
if(month==1) return 'February';
if(month==2) return 'March';
if(month==3) return 'April';
if(month==4) return 'May';
if(month==5) return 'June';
if(month==6) return 'July';
if(month==7) return 'August';
if(month==8) return 'September';
if(month==9) return 'October';
if(month==10) return 'November';
if(month==11) return 'December';
}
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
var currentMonthName = returnMonth(currentDate.getMonth());
var currentDayOfMonth = currentDate.getDate();
var currentYear = currentDate.getYear();
var currentTime = currentDate.getHours() + ':' + currentDate.getMinutes();
var currentDay = returnDay(currentDate.getDay());
document.getElementById('date').innerHTML = 'Current Date is = ' + currentDate;
document.getElementById('date').innerHTML = document.getElementById('date').innerHTML + '
Current Month is = ' + (currentMonth+1<10 ? '0'+(currentMonth+1) : currentMonth+1);
document.getElementById('date').innerHTML = document.getElementById('date').innerHTML + '
Current Month Name is = ' + (currentMonthName);
document.getElementById('date').innerHTML = document.getElementById('date').innerHTML + '
Current Day is = ' + (currentDayOfMonth);
document.getElementById('date').innerHTML = document.getElementById('date').innerHTML + '
Current Day Name is = ' + (currentDay);
document.getElementById('date').innerHTML = document.getElementById('date').innerHTML + '
Current Year is = ' + (currentYear);
document.getElementById('date').innerHTML = document.getElementById('date').innerHTML + '
Current Time is = ' + (currentTime);
Like
Dislike
Keywords for this article:
javascript || getting the month || getting the month name || getting the year || getting the date || getting the day name
Advertisement by Google
Comment:
Code Language:
Code:
Here you can paste a code example. It will then be processed by SyntaxHighlighter and formatted for easier readability.
Please remember to select the correct Code Language in the select above so the SyntaxHighlighter can highlight the code properly.
Code:
Please enter the code you see above
What is 7 + 10 =