Javascript 相当于 php mktime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15062285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:20:39  来源:igfitidea点击:

Javascript equivalent of php mktime

javascript

提问by Gautam3164

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

我在 php 中使用 mktime() 函数来获取给定年、月、日和分钟的秒数

$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.

但我想在 javascript 中使用相同的......谁能建议我在 javascript 中使用它的等效函数的方法,它接受所有参数并返回秒数......我搜索了很多来源,但没有人给我输出。

回答by bart s

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

以上将给出自 1-1-1970 以来的秒数。getTime()给出毫秒,因此除以 1000。注意(正如 Aler Close 也提到的),月份的范围是 0-11,因此与mktime

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

回答by Manishearth

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

回答by Aler Close

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

您可以使用 Date 对象(months 是一个从 0 到 11 的值):

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