Lib: https://momentjs.com/
Date Formatting
Using Moment.js to format daté and times helps you write code simpler and much easier.
The simple example is as follows:
moment().format('YYYY MM DD'); // current date time have format YYYY MM DD
moment("2017-01-01").format("DD MMM YYYY") // 01 Jan 2017
The format of date times, you can find more in the docs của moment.js.
moment().format('YYYY MM DD'); // current date time have format YYYY MM DD
moment("2017-01-01").format("DD MMM YYYY") // 01 Jan 2017 Date Validation
Use moment.js to confirm that the date time entered is in the correct format as date time.
Using the isValid () method, returns true / false.
moment("2010 13", "YYYY MM").isValid(); // false (invalid month)
moment("2010 11 31", "YYYY MM DD").isValid(); // false (invalid day)
moment("2010 2 29", "YYYY MM DD").isValid(); // false (not is leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (invalid name of the month)
Some note that the moment.js parser is quite forgiving :), even if it analyzes that a certain word in the date time is correct, it will return isValid () to true.
moment('2016 is a date', 'YYYY-MM-DD').isValid() //true, 2016 was matched
moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true 2012-05-25 matched
If you need the parser of the moment more closely, you must add the boolean as the final argument.
moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true
moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false
moment('2012-05-25', 'YYYY-MM-DD', true).isValid(); // true
moment('2012.05.25', 'YYYY-MM-DD', true).isValid(); // false
To know where it is invalid, you can use the function invalidAt () as follows:
moment("2014-14-01").invalidAt(); // 1 => month
moment("2014-01-32").invalidAt(); // 2 => day
Its return value:
1: years
2: months
3: days
4: hours
5: minutes
6: seconds
7: milliseconds
Using the isValid () method, returns true / false.moment("2010 13", "YYYY MM").isValid(); // false (invalid month)
moment("2010 11 31", "YYYY MM DD").isValid(); // false (invalid day)
moment("2010 2 29", "YYYY MM DD").isValid(); // false (not is leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (invalid name of the month)
moment('2016 is a date', 'YYYY-MM-DD').isValid() //true, 2016 was matched
moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true 2012-05-25 matched
moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true
moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false
moment('2012-05-25', 'YYYY-MM-DD', true).isValid(); // true
moment('2012.05.25', 'YYYY-MM-DD', true).isValid(); // false
moment("2014-14-01").invalidAt(); // 1 => month
moment("2014-01-32").invalidAt(); // 2 => day
1: years
2: months
3: days
4: hours
5: minutes
6: seconds
7: milliseconds
Manipulating Dates
Add
moment().add(Number, String);
moment().add(Duration);
moment().add(Object);
ví dụ:
moment (), add (7, 'days'); // Returns an object that has a date time of 7 days
moment (). add (7, 'd'); / acronym as above
Key Shorthand
years y
quarters Q
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms
moment().add(7, 'days').add(1, 'months'); // with chaining
moment().add(Number, String);
moment().add(Duration);
moment().add(Object);
| Key | Shorthand |
|---|---|
| years | y |
| quarters | Q |
| months | M |
| weeks | w |
| days | d |
| hours | h |
| minutes | m |
| seconds | s |
| milliseconds | ms |
moment().add(7, 'days').add(1, 'months'); // with chaining
Subtract
moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
Ex:
moment().subtract(7, 'days');
moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
moment().subtract(7, 'days');
Start of Time
moment().startOf('year'); // set to January 1st, 12:00 am this year
moment().startOf('month'); // set to the first of this month, 12:00 am
moment().startOf('quarter'); // set to the beginning of the current quarter, 1st day of months, 12:00 am
moment().startOf('week'); // set to the first day of this week, 12:00 am
moment().startOf('isoWeek'); // set to the first day of this week according to ISO 8601, 12:00 am
moment().startOf('day'); // set to 12:00 am today
moment().startOf('date'); // set to 12:00 am today
moment().startOf('hour'); // set to now, but with 0 mins, 0 secs, and 0 ms
moment().startOf('minute'); // set to now, but with 0 seconds and 0 milliseconds
moment().startOf('second'); // same as moment().milliseconds(0);
moment().startOf('year'); // set to January 1st, 12:00 am this year
moment().startOf('month'); // set to the first of this month, 12:00 am
moment().startOf('quarter'); // set to the beginning of the current quarter, 1st day of months, 12:00 am
moment().startOf('week'); // set to the first day of this week, 12:00 am
moment().startOf('isoWeek'); // set to the first day of this week according to ISO 8601, 12:00 am
moment().startOf('day'); // set to 12:00 am today
moment().startOf('date'); // set to 12:00 am today
moment().startOf('hour'); // set to now, but with 0 mins, 0 secs, and 0 ms
moment().startOf('minute'); // set to now, but with 0 seconds and 0 milliseconds
moment().startOf('second'); // same as moment().milliseconds(0);
End of Time
moment().endOf("year"); // set the moment to 12-31 23:59:59.999 this year
moment().endOf("year"); // set the moment to 12-31 23:59:59.999 this year
Time from now
moment().fromNow(); // time ago
moment().fromNow(Boolean); // relative time
moment("2012-01-01").fromNow(); // 6 years ago
moment("2012-01-01").fromNow(true); // 6 years
moment().fromNow(); // time ago
moment().fromNow(Boolean); // relative time
moment("2012-01-01").fromNow(); // 6 years ago
moment("2012-01-01").fromNow(true); // 6 years
Time from X
moment().from(Moment|String|Number|Date|Array);
moment().from(Moment|String|Number|Date|Array, Boolean);
var a = moment([2007, 0, 28]); // can write array or string
var b = moment([2007, 0, 29]); / can write array or string
a.from(b) // "a day ago"
moment().from(Moment|String|Number|Date|Array);
moment().from(Moment|String|Number|Date|Array, Boolean);
var a = moment([2007, 0, 28]); // can write array or string
var b = moment([2007, 0, 29]); / can write array or string
a.from(b) // "a day ago"
Difference
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
Days in Month
moment("2012-02", "YYYY-MM").daysInMonth() // 29
moment("2012-01", "YYYY-MM").daysInMonth() // 31
moment("2012-02", "YYYY-MM").daysInMonth() // 29
moment("2012-01", "YYYY-MM").daysInMonth() // 31
Is Leap Year
Check is leap year
moment([2000]).isLeapYear() // true
moment([2001]).isLeapYear() // false
moment([2100]).isLeapYear() // false
moment([2000]).isLeapYear() // true
moment([2001]).isLeapYear() // false
moment([2100]).isLeapYear() // false
Get
moment().get('year');
moment().get('month'); // 0 to 11
moment().get('date');
moment().get('hour');
moment().get('minute');
moment().get('second');
moment().get('millisecond');
moment().get('year');
moment().get('month'); // 0 to 11
moment().get('date');
moment().get('hour');
moment().get('minute');
moment().get('second');
moment().get('millisecond');
No comments:
Write comments