javascriptphpjs

JavaScript Dates and Times


What is the JavaScript equivalent to the following PHP code:

$expTime = time() + (5 * 60 * 60); // now plus 5 hours (5 hour; 60 mins; 60 secs) 
$expTimeStr = gmdate('Y-m-d\TH:i:s\Z', $expTime); 

Solution

  • var expTime    = new Date((+new Date()) + (5 * 60 * 60000))
    var m          = expTime.getMonth() + 1
    var d          = expTime.getDate()
    var y          = expTime.getFullYear()
    var h          = expTime.getHours()
    var i          = expTime.getMinutes()
    var s          = expTime.getSeconds()
    var expTimeStr = y +"-"+ m +"-"+ d +" "+ h +":"+ i +":"+ s