0

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

user1995927
  • 313
  • 1
  • 3
  • 11

5 Answers5

1

Since I made comments on almost all answers, I'd better post my suggestion

DEMO

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
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
  • 1
    I 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