Javascript equivalent php mktime

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

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

but I want to use the same in javascript ... can anyone suggest me a way to use my equivalent function in javascript that takes above all parameters and returns the number of seconds ... I searched for so many sources, but no one gave me the result.

+7
source share
3 answers
 var seconds = new Date(year, month, day, hours, minutes, seconds, 0).getTime() / 1000; 

The above will give seconds from 1-1 to 1970. getTime() gives milliseconds, therefore it is divided by 1000. Note (as mentioned by Aler Close), the month varies from 0-11, so you may need to fix this compared to mktime

 function java_mktime(hour,minute,month,day,year) { return new Date(year, month - 1, day, hour, minutes 0, 0).getTime() / 1000; } 
+12
source

use 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 } 

As an alternative,

  function mktime(hour,minute,month,day,year){ return (new Date(year, month, day, hour, minute, 0)).getTime()/1000; } 
+6
source

You can use a Date object (months - a value from 0 to 11):

 var date = new Date(year, month, day, hours, minutes, seconds); 
0
source

All Articles