Javascript 相当于 php 的 strtotime()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4048204/
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
Javascript equivalent of php's strtotime()?
提问by VJS
In PHP, you can easily convert an English textual datetime description into a proper date with strtotime()
.
在 PHP 中,您可以轻松地将英文文本日期时间描述转换为正确的日期strtotime()
。
Is there anything similar in Javascript?
Javascript中有没有类似的东西?
采纳答案by usr-local-ΕΨΗΕΛΩΝ
I found this articleand tried the tutorial. Basically, you can use the date constructor to parse a date, then write get the seconds from the getTime()
method
我找到了这篇文章并尝试了教程。基本上,您可以使用日期构造函数来解析日期,然后从getTime()
方法中写入获取秒数
var d=new Date("October 13, 1975 11:13:00");
document.write(d.getTime() + " milliseconds since 1970/01/01");
Does this work?
这行得通吗?
回答by Arnaldo
Yes, it is. And it is supported in all major browser:
是的。并且所有主流浏览器都支持它:
var ts = Date.parse("date string");
The only difference is that this function returns milliseconds instead of seconds, so you need to divide the result by 1000.
唯一的区别是这个函数返回的是毫秒而不是秒,所以你需要将结果除以 1000。
回答by Westy92
Check out thisimplementation of PHP's strtotime() in JavaScript!
看看这个PHP 的 strtotime() 在 JavaScript 中的实现!
I found that it works identically to PHP for everything that I threw at it.
我发现它对我投入的所有东西都与 PHP 相同。
Update: this function as per version 1.0.2 can't handle this case:
'2007:07:20 20:52:45'
(Note the:
separator for year and month)
更新:1.0.2 版本的这个函数无法处理这种情况:(
'2007:07:20 20:52:45'
注意:
年和月的分隔符)
Update 2018:
2018 年更新:
This is now available as an npm
module! Simply npm install locutus
and then in your source:
现在可以作为npm
模块使用了!简单地npm install locutus
然后在您的来源中:
var strtotime = require('locutus/php/datetime/strtotime');
回答by KhoPhi
I jealous the strtotime() in php, but I do mine in javascript using moment. Not as sweet as that from php, but does the trick neatly too.
我嫉妒 php 中的 strtotime(),但我使用 moment 在 javascript 中做我的。不像 php 那样甜蜜,但也巧妙地完成了这个技巧。
// first day of the month
var firstDayThisMonth = moment(firstDayThisMonth).startOf('month').toDate();
Go back and forth using the subtract()
and add()
with the endOf()
and startOf()
:
来回使用subtract()
,并add()
与endOf()
和startOf()
:
// last day of previous month
var yesterMonthLastDay = moment(yesterMonthLastDay).subtract(1,'months').endOf('month').toDate();
回答by doc_id
回答by Adnane Ar
Maybe you can exploit a sample function like :
也许您可以利用以下示例函数:
function strtotime(date, addTime){
let generatedTime=date.getTime();
if(addTime.seconds) generatedTime+=1000*addTime.seconds; //check for additional seconds
if(addTime.minutes) generatedTime+=1000*60*addTime.minutes;//check for additional minutes
if(addTime.hours) generatedTime+=1000*60*60*addTime.hours;//check for additional hours
return new Date(generatedTime);
}
let futureDate = strtotime(new Date(), {
hours: 1, //Adding one hour
minutes: 45 //Adding fourty five minutes
});
document.body.innerHTML = futureDate;
`
`
回答by Limitless isa
var strdate = new Date('Tue Feb 07 2017 12:51:48 GMT+0200 (Türkiye Standart Saati)');
var date = moment(strdate).format('DD.MM.YYYY');
$("#result").text(date); //07.02.2017
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
<div id="result"></div>
回答by bedrin
Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
浏览器对解析字符串的支持不一致。因为没有关于应该支持哪些格式的规范,所以在某些浏览器中有效的方法在其他浏览器中无效。
Try Moment.js- it provides cross-browser functionality for parsing dates:
试试Moment.js- 它提供了用于解析日期的跨浏览器功能:
var timestamp = moment("2013-02-08 09:30:26.123");
console.log(timestamp.milliseconds()); // return timestamp in milliseconds
console.log(timestamp.second()); // return timestamp in seconds