I have trouble using date in Javascript, in PHP you use something like date("Y-m-d H:i s") to retrieve the specific date and time, how can I achieve this in Javascript? the getMonth() method only returns 1 digit, I really need them to be in 2 digits
Asked
Active
Viewed 166 times
0
-
so you pad: if (mm<10) mm="0"+mm; – mplungjan Mar 17 '13 at 05:44
-
Can you try my answer? – Dead Man Mar 17 '13 at 05:46
-
@mplungjan, i know man, i just want to let him know, that's it. :) – Dead Man Mar 17 '13 at 05:48
-
possible duplicate of [How do i get Month and Date of Javascript in two digit format](http://stackoverflow.com/questions/6040515/how-do-i-get-month-and-date-of-javascript-in-two-digit-format) – mplungjan Mar 17 '13 at 05:53
5 Answers
1
Since I made comments on almost all answers, I'd better post my suggestion
function pad(num) { return ("0"+num).slice(-2); }
function getDisplayDate() {
var date = new Date();
return date.getFullYear()+
"-"+pad(date.getMonth()+1)+
"-"+pad(date.getDate())+
" "+pad(date.getHours())+
":"+pad(date.getMinutes())+
":"+pad(date.getSeconds());
}
setInterval(function() {
document.getElementById("time").innerHTML=getDisplayDate();
},500);

mplungjan
- 169,008
- 28
- 173
- 236
0
why dont you add 0 before when you get <10

LynAs
- 6,407
- 14
- 48
- 83
-
Choices .. :) ... I altered the code, which made your argument a valid addition. thanks ! – Daniel Mar 17 '13 at 07:09
0
Try this :
function dateToYMD(date) {
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d) + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
}
var d = new Date();
alert(dateToYMD(d));

Dead Man
- 2,880
- 23
- 37
0
This code is adjusted based on the pointers given by @mplungjan -- (credits to him please) Also check out his solution, which is a better one for use with date digits.
var str = 10; // month-digit from getMonth()
function pad(val) {
return "0" + val;
}
var month = str < 10 ? pad(str) : str;
console.log(month);

Daniel
- 4,816
- 3
- 27
- 31
-
Well... I am not familiar with "pad" and can't find this back as a a native function, so you would still nee to declare this function. I did alter my code to make it shorter .. :) – Daniel Mar 17 '13 at 07:03
-
Well dear friend .. You write a function and return val and then return "0"+num. That already misses the "else" statement, so I apologize if I get confused. I just don't see what you are getting at. So all I can say is that I will appreciate your solution worked out. I am always open to learn new ways. Now you vote me down, but you offer me nothing new. If I overlook something, then please specify. – Daniel Mar 17 '13 at 07:46
-
`function pad(num) { if (num>9) return num; return "0"+num}` - I was on my iPhone - a typo. No need for else when you return – mplungjan Mar 17 '13 at 08:36
-
1I voted you down because you hugely complicated the issue, still use getMonthDigits when your function no longer is visibly related to month, do not define month and so on. – mplungjan Mar 17 '13 at 08:38
-
@mplungjan - Thanks. A valid point not to define "month". Omitting "else" is more a shortcut I see. A nice way to keep things short. Thanks. – Daniel Mar 17 '13 at 16:54
-
Normally you would do `var a = ()?b:c` but you have complicated things with the abuse of the ternary side effects of `()?a=b:a=c` – mplungjan Mar 17 '13 at 17:18
-
@mplungjan - Thanks for the lesson. I adjusted the code to reflect on lessons learned and to eliminate the "horror". – Daniel Mar 17 '13 at 20:37
-2
you can year, minutes, etc from Date class. You can get 2 digits month using some trick like example below. e.g
Date.prototype.getLongMonth = function() {
var month = this.getMonth();
if (String(month).length == 1) month = '0'.concat(month);
return month;
}
var now = new Date();
var theDate = now.getFullYear() + '-' + now.getLongMonth() + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
console.log(theDate); // result : 2013-02-17 12:41:2

novalagung
- 10,905
- 4
- 58
- 82
-
1He said now.getMonth() this will return month in 1 digit, he wants in 2 digits – Dead Man Mar 17 '13 at 05:42
-
2That is a LOT more code than `if (mm<10) mm="0"+mm` and it is wrong because it will return 00 for January – mplungjan Mar 17 '13 at 05:48
-
-
-
1I've suggested to use the solution [("0" + (this.getMonth() + 1)).slice(-2)](http://stackoverflow.com/a/6040556/295783) – mplungjan Mar 17 '13 at 05:57