Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript equivalent of php mktime

Tags:

javascript

Iam using mktime() function in php to get the seconds for given year,month,date and minutes as like

$seconds = mktime($hour,$minute,$month,$day,$year);

but I want to use the same in javascript...can anyone suggest me the way to use its equivalent function in javascript that takes above all parameters and returns number of seconds...I have searched so many sources but no one has given me the output.

like image 727
Gautam3164 Avatar asked Feb 25 '13 07:02

Gautam3164


1 Answers

var seconds = new Date(year, month, day, hours, minutes, seconds, 0).getTime() / 1000;

The above will give seconds since 1-1-1970. getTime() gives miliseconds therefore devide by 1000. Note (as Aler Close also mentioned), the month ranges from 0-11, so you might need to correct that compared to mktime

function java_mktime(hour,minute,month,day,year) {
    return new Date(year, month - 1, day, hour, minutes 0, 0).getTime() / 1000;
}
like image 131
bart s Avatar answered Nov 07 '22 08:11

bart s