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.

Không có nhận xét nào:

StaticImage

  import React , { useEffect , useRef } from "react" import { StaticImage } from "gatsby-plugin-image" impor...