Moment.js is awesome, it lets you format dates to whatever format you need. But is there any function that allows you to list all the weekend dates (in DOY format) for the particular year?
it can be done easily using a JS loop and adding dates functionality of moment.js. Take an year set date to 1th of Jan., check the weekday and then go to next date by momentjs function add(1, 'days')
hope it helps
Related
how do I save the day of the week to array from a given date range. For example, from 8/23/2021 to 12/11/2021, I would like to get the dates that are Mondays and save these dates to an array? Thanks
So you can use the getDay method.
Given your date:
var someday = new Date('8/23/2021');
using the getDay against the someday will return the day of the week index. 1 in this case.
Edit (Side-note): Looks like Sunday is the start of the week for JS
We can pass some options to get the proper long day and formatting.
var long_day = new Intl.DateTimeFormat('en-US', { weekday: 'long'}).format(someday);
This will return, Monday. You could save it when its 1/Monday, up to you.
JSFiddle
From the docs for Moment.js, it says moment().startOf('isoWeek') sets to the first day of the week according to ISO 8601.
From the wikipedia page for ISO 8601, it says the first week is (among other equivalent definitions): the first week with the majority (four or more) of its days in the starting year.
From the docs for MySQL, it says WEEK(date, 3) gives a week (1-53) with Monday as the first day of the first week with 4 or more days this year.
From what I can tell, these are all equivalent definitions of a week, yet they don't mesh.
My MySQL database (v5.6.17) gives me 201616 for SELECT YEARWEEK('2016-4-20',3)
Using Moment.js (v2.10.3) in the browser gives me Mon Apr 11 2016 for moment().year(2016).week(16).startOf("isoweek")
Those aren't the same week, and I can't figure out why.
When you call .week() you are setting the "locale" week (I have no idea how moment.js works that out), but likely the first day will be Sunday. When you then call .startOf('isoweek') you are then getting the ISO start of that week, which is the previous Monday.
If you want to use ISO, you have to use ISO methods, so:
moment().year(2016).isoWeek(16).startOf('isoweek');
creates a moment object for 2016-04-18T00:00:00
Or use a string:
moment('2016-W16') // 2016-04-18T00:00:00
which defaults to ISO week numbering and start of week.
I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js.
This is what I have so far 01:00 2016-01-01, I need to format or convert that to ISO. Is there a way to do it using Moment?
To convert ISO I recommend the more standard
date.format();
or
JSON.stringify(yourDate)
or if you prefer momentjs:
var date = moment();
date.toISOString();
or
moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string
To know what are the momentjs formatting rules start reading here
Assuming you are referring to ISO8601 and momentjs (2.10.6), I currently do it like this
var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");
You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):
var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");
Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:
myMoment.format();
See the moment docs for more info here.
Hope this helps!
There is the following code:
console.log(order.start_time);
console.log(moment(order.start_time).format("HH:MM"));
I just want to get hour and minute from date using moment and display it. Output:
2014-06-30T09:00:00.000Z
13:06
But I don't understand why my date is formatted incorrectly. Thanks in advance.
Please note that there is a difference between MM and mm. The first formatting option is used for months, while the second is for days.
More information here: http://momentjs.com/docs/#/parsing/string-format/
From the doc here:
http://momentjs.com/docs/#/displaying/format/
You should use HH:mm. Then your time will have right format.
Further information, the time might be incorrectly as you expected because timezone. Momentjs auto display time in your system timezone.
Update:
To display time in specified Timezone, use "zone" method:
console.log(moment(order.start_time).zone('+00').format("HH:mm"));
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 fiddle.
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)
Maybe a little late to the party, but here's the proper way to do this, as in the documentation.
moment().day(1); // Monday in the current week
Also if in your locale it happens that the week starts with Monday and you wish to get a locally aware result, you can use moment().weekday(0). Here's the documentation for the moment().weekday(dayNumber) method.
It's not longer possible to use just a string (e. g. 'isoweek'), we need to use it like this:
import * as moment from 'moment';
import { unitOfTime } from 'moment';
moment().startOf('isoweek' as unitOfTime.StartOf);