Javascript 用momentJS比较2个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37799624/
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
Comparing 2 dates with momentJS
提问by Geraint
I am trying to compare a DAY/TIME e.g. Monday 09:00:00 with the current time to see if I am past that point in the week. e.g. If it is now 05:00:00 on Monday it should return true however it is returning false everytime
我正在尝试将 DAY/TIME(例如,星期一 09:00:00)与当前时间进行比较,以查看我是否超过了一周中的那个时间点。例如,如果现在是周一的 05:00:00,它应该返回 true 但是它每次都返回 false
var dayTime = Moment("Wednesday 17:00:00", "dddd HH:mm:ss");
var now = Moment(Moment.now(), "dddd HH:mm:ss");
console.log(Moment.utc(dayTime).isBefore(now)); //returns false all the time
I found the following similar questions but it didn't seem to fix the issue after formatting the time.
我发现了以下类似的问题,但在格式化时间后似乎没有解决问题。
Comparing two times with Moment JS
When I replace the moment.now()
with a string such as "Wednesday 17:00:00" it returns the expected result.
当我用moment.now()
诸如“Wednesday 17:00:00”之类的字符串替换它时,它会返回预期的结果。
Any idea what I need to do to moment.now()
in order for this to work correctly?
知道我需要做什么moment.now()
才能使其正常工作吗?
采纳答案by Geraint
For anyone who is interested, the code I posted in my question was changing the day/hour but was setting the year to 2015 which meant that it was always in the past.
对于任何感兴趣的人,我在问题中发布的代码正在更改日期/小时,但将年份设置为 2015 年,这意味着它始终是过去。
To fix I separated out the Day and the Hour and set to moment. Then compared that with now. e.g.
为了解决这个问题,我将日期和时间分开并设置为时刻。然后和现在相比。例如
moment().set({"Day": "Friday", "Hour": "17"}).isBefore(moment())
回答by Maggie Pint
Moment.now can be used as an extension point, but it's really not a public API. To get the current time in momentjs you just call moment()
. Note that all moment calls use lowercase moment
.
Moment.now 可以用作扩展点,但它实际上不是公共 API。要在 momentjs 中获取当前时间,您只需调用moment()
. 请注意,所有时刻调用都使用小写moment
。
To see if your date and time is before the current time, you would just call:
要查看您的日期和时间是否在当前时间之前,您只需调用:
moment('01/01/2016', 'MM/DD/YYYY').isBefore(moment())
You would replace the date and format in question with your own.
您可以用您自己的日期和格式替换有问题的日期和格式。
I see that you have a date format that includes only day of week and time. Moment will parse this, but be aware that the behavior might not be what you expect. When I parse your date, I get Wednesday December 30, 2015. Exactly what day this lands on will vary by locale. In any case, I doubt that it is what you want. If at all possible, I would get year, month, and day.
我看到您的日期格式仅包含星期几和时间。Moment 会对此进行解析,但请注意该行为可能不是您所期望的。当我解析你的日期时,我得到 2015 年 12 月 30 日星期三。具体哪一天会因地区而异。无论如何,我怀疑这是你想要的。如果可能的话,我会得到年、月和日。
If you would like to instead set the moment to Wednesday this week, set the day on the moment using .day()
. For instance:
如果您想将这一时刻设置为本周的星期三,请使用 将这一时刻设置为星期三.day()
。例如:
moment().day(3).format()
"2016-06-15T20:19:55-05:00"