使用Moment.js查找当前周的特定日期 [英] Using Moment.js to find a specific day of the current week's date
问题描述
使用Moment.js查找当周的特定日期的日期
有很多方法来操作JavaScript中的日期。我一直在寻找最简单,最简单的方式来做到这一点,而不用长时间,丑陋的代码,所以有人向我展示了 Moment.js 。
我想使用当前日期来查找此图书馆当前周的特定日期的日期。到目前为止,我的尝试涉及到当前日数(天0-6)之间的差异,并检查它与星期一(第1天)之间的天数,这是不对的。
这是我的代码:
var now = moment();
/ pre>
var day = now.day();
var week = [['sunday',0],['monday',1],['tuesday',2],['wndnesday',3],['thursday',4 ],['星期五',5],['星期六',6]];
var monday = moment()。day( - (week [1] [1] - day)); //今天减去星期一和今天之间的差额
$ (#console)。text(monday);
//我需要知道本周星期一的日期
//我需要知道当周星期五的日期
我该怎么做?我的方法可能是一个可怕的方式来完成这个工作,或者它可能有点接近。不管怎样,我们都希望解决方案整齐,小巧,动态,简单。
我不想使用本机JS日期功能,在我看到的每种情况下都会产生丑陋,凌乱的代码。
解决方案本周的星期天
moment()一周')
本周星期一
moment()。startOf('isoweek')
星期六
moment()。endOf('week')
当天到星期日之间的差异
moment()。diff (moment()。startOf('week'),'days')
本周的wedesday
moment()。startOf('week')。add('days',3)
Finding the date of a specific day of the current week with Moment.js
There are lots of ways to manipulate dates in javascript. I've been looking for the simplest, easiest way to do so without long, ugly code, so someone showed me Moment.js.
I want to use the current date to discover the date of a specific day of the current week with this library. My attempt so far involves taking the difference between the current day number(days 0-6) and checking how many days are between it and monday(day 1), which is not right at all.
Here's my code:
var now = moment(); var day = now.day(); var week = [['sunday',0],['monday',1],['tuesday',2],['wednesday',3],['thursday',4],['friday',5],['saturday',6]]; var monday = moment().day(-(week[1][1] - day));//today minus the difference between monday and today $("#console").text(monday); //I need to know the date of the current week's monday //I need to know the date of the current week's friday
How can I do this? My method may be a terrible way to get this done, or it might be somewhat close. I do, however want the solution to be neat, small, dynamic, and simple, as all code should be.
I'd prefer not to use native JS date functionality which produces ugly, messy code in every situation that I've seen.
解决方案this week's sunday
moment().startOf('week')
this week's monday
moment().startOf('isoweek')
this week's saturday
moment().endOf('week')
difference between the current day to sunday
moment().diff(moment().startOf('week'),'days')
this week's wedesday
moment().startOf('week').add('days', 3)
这篇关于使用Moment.js查找当前周的特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!