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.
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.
Share Improve this question edited Feb 25, 2013 at 7:53 Cory Danielson 14.5k4 gold badges46 silver badges52 bronze badges asked Feb 25, 2013 at 7:48 GautamD31GautamD31 28.8k10 gold badges65 silver badges86 bronze badges 1- 1 phpjs.org/functions/mktime – dfsq Commented Feb 25, 2013 at 7:51
3 Answers
Reset to default 13var 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;
}
use the Date object
function mktime(hour,minute,month,day,year){
a=new Date()
a.setHours(hour)
a.setMinutes(minute)
a.setDate(day)
a.setYear(year)
return a.getTime()/1000
}
Alternatively,
function mktime(hour,minute,month,day,year){
return (new Date(year, month, day, hour, minute, 0)).getTime()/1000;
}
You can use Date object (months is a value from 0 to 11):
var date = new Date(year, month, day, hours, minutes, seconds);