Well...to begin with...let me say I have done this a thousand times. I am simply trying to print utc time in JavaScript.
But...the value that I got is wrong.
Instead of ( 9) September, JavaScript is returning (8) for august. Since today is September 2,2014.
The UTC time would be something like: 2014-09-02 07:00:02
.
Instead of that I am getting 2014-08-02 07:00:02
.
I have included a fiddle.Please have a look at it.
FIDDLE
Well...to begin with...let me say I have done this a thousand times. I am simply trying to print utc time in JavaScript.
But...the value that I got is wrong.
Instead of ( 9) September, JavaScript is returning (8) for august. Since today is September 2,2014.
The UTC time would be something like: 2014-09-02 07:00:02
.
Instead of that I am getting 2014-08-02 07:00:02
.
I have included a fiddle.Please have a look at it.
FIDDLE
Share Improve this question asked Sep 2, 2014 at 7:02 user3962692user3962692 1- 4 its not really wrong i think, the count of month in javascript starts with zero means january is zero – Netorica Commented Sep 2, 2014 at 7:03
3 Answers
Reset to default 16Months in JavaScript are returned as a 0 based value.
0 January
1 Feburary
...
8 September
9 November
...
Docs:
The value returned by getUTCMonth is an integer between 0 and 11 corresponding to the month. 0 for January, 1 for February, 2 for March, and so on.
The getMonth() method returns the month (from 0 to 11) for the specified date, according to universal time.
You can try using array.
var d = new Date()
var month = new Array(12);
month[0] = "January";
month[1] = "February";
...
var n = month[d.getUTCMonth()];
Java script use 0 for January so every month will give you int value of the previous month.
So you can just add 1 in the variable where you are storing the month value and it i will work perfectly. e.g :
document.getElementById("date").innerHTML = date + ":" + (month+1) + ":" + year;
If you wanna display date then you can see the below example code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Clock</title>
<style>
#date{
color: red;
font-size: 20px;
text-align: center;
}
</style>
</head>
<body>
<div id = "date"></div>
<script>
setInterval(displayclock,500);
function displayclock () {
var time = new Date ();
var date = time.getUTCDate();
var month = time.getUTCMonth();
month = month + 1;
var year = time.getFullYear();
if (month < 10){
month = "0" + month
}
if (date < 10){
date = "0" + date
}
document.getElementById("date").innerHTML = date + ":" + month + ":" + year;
}
</script>
</body>
</html>