typescript 比较Angular 6中的两个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53996410/
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
compare two dates in Angular 6
提问by Zhu
I am new to angular 6 ,Here I need to compare to date inputs and find the greatest one.
我是 angular 6 的新手,在这里我需要比较日期输入并找到最大的输入。
input 1 : 2018-12-29T00:00:00
input 2 : Mon Dec 31 2018 00:00:00 GMT+0530 (India Standard Time)
Here I received the input 1from mssql database and the input 2from the material datepicker .
在这里,我收到了来自 mssql 数据库的输入 1和来自材料 datepicker的输入 2。
while compare this two dates as below I got false.
在比较这两个日期如下时,我得到了错误。
console.log(mMagazineObject.From < mMagazineObject.To ? true : false);
is there any possibility to compare these two date formats .If yes please help me to fix this .
有没有可能比较这两种日期格式。如果是,请帮我解决这个问题。
回答by Dervi? Kay?mba??o?lu
you can use getTime
你可以使用 getTime
if (input1Date.getTime() < input2Date.getTime())
Note that if your dates are in string format, you first need to parse them to Date
请注意,如果您的日期是字符串格式,则首先需要将它们解析为 Date
回答by Zhu
finally I found the solution.
最后我找到了解决方案。
console.log(mMagazineObject.From < this.datePipe.transform(mMagazineObject.To, 'yyyy-MM-dd') ? true : false);
回答by Sam96
Using Date.parse(input)
is the best idea for if the APIs change. This provides timestamps, numbers which you can compare easily with the math comparisons.
Date.parse(input)
如果 API 发生变化,使用是最好的主意。这提供了时间戳、数字,您可以轻松地将它们与数学比较进行比较。