Hiển thị các bài đăng có nhãn moment. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn moment. Hiển thị tất cả bài đăng

Moment duration

Source: https://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes/34672015

moment.duration(now.diff(then)).humanize()


public getDurationText(date1, date2) {
if (!date1) {
return '';
}
// check null date2 (is current date)
const date2Tmp = date2 ? date2 : new Date();
// return
return moment.duration(moment(date2Tmp).diff(moment(date1))).humanize();
}

Moment: How to prevent “Invalid date”?

Source: https://stackoverflow.com/questions/28993107/momentjs-how-to-prevent-invalid-date


If your goal is to find out whether you have a valid date, use Moment's isValid:
var end_date_moment, end_date;
jsonNC.end_date = jsonNC.end_date.replace(" ", "T");
end_date_moment = moment(jsonNC.end_date);
end_date = end_date_moment.isValid() ? end_date_moment.format("L") : "";
...which will use "" for the end_date string if the date is invalid.

Moment format pattern cheatsheet

https://devhints.io/moment

Date

d0..6Weekday
ddSu
dddSun
ddddSunday
YY13Year
YYYY2013
M1..12 (Jan is 1)Month
Mo1st..31st
MM01..12 (Jan is 1)
MMMJan
MMMMJanuary
Q1..4Quarter
Qo1st..4th
D1..31Day
Do1st..31st
DD01..31
DDD1..365Day of year
DDDo1st..365th
DDDD001..365
w1..53Week of year
wo1st..53rd
ww01..53


Time

H0..2324h hour
HH00..23 
h1..1212h hour
hh01..12 
m0..59Minutes
mm00..59 
s0..59Seconds
ss00..59 
aamAM/PM
AAM 
Z+07:00Timezone offset
ZZ+0730 
S0..9Deciseconds
SS00..99Centiseconds
SSS000..999Milliseconds
X
 x
 Unix timestamp
Miliseconds Unix timestamp

Presets

LT8:30 PM
LTS8:30:25 PM
LLAugust 2 1985
llAug 2 1985
LLLAugust 2 1985 08:30 PM
lllAug 2 1985 08:30 PM
LLLLThursday, August 2 1985 08:30 PM
llllThu, Aug 2 1985 08:30 PM

Examples

Date

YYYY-MM-DD2014-01-01
dddd, MMMM Do YYYYFriday, May 16th 2014

Time

hh:mm a12:30 pm
Used by Moment.js and date-fns/format. Similar to Java SimpleDateFormat.

Get Date object from Moment

Source: https://stackoverflow.com/questions/17987647/moment-js-transform-to-date-object

Use this to transform a moment object into a date object:
moment().toDate();
Yields:
$(function(){
  setInterval(function(){
    var divUtc = $('#divUTC');
    var divLocal = $('#divLocal');  
    //put UTC time into divUTC  
    divUtc.text(moment.utc().format('YYYY-MM-DD HH:mm:ss'));      
    
    //get text from divUTC and conver to local timezone  
    var localTime  = moment.utc(divUtc.text()).toDate();
    localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
    divLocal.text(localTime);  
      
    $('#divThai').text(moment.tz('Asia/Bangkok').format('YYYY-MM-DD HH:mm:ss'));
    $('#divUsa').text(moment.tz('America/Los_Angeles').format('YYYY-MM-DD HH:mm:ss'));
  },1000);
});
Tue Nov 04 2014 14:04:01 GMT-0600 (CST)
var myDateObj = new Date(2011, 9, 16);
var now = moment(myDateObj);
#Now convert it back to date object
var newDateObj = new Date(now.format("YYYY-MM-DDTHH:mm:ssZ"));
This might be a delayed response.But, I think it can help others who still needs an answer.
To retrieve a native Date object from Moment, use .toDate()
You can directly get the Date object from Moment.

anti-pattern là gì

  Trong công nghệ và lập trình, Anti-pattern (mẫu phản diện) là những giải pháp bề ngoài có vẻ hiệu quả để giải quyết một vấn đề phổ biến, ...