Moment.js transform to date object
Use this to transform a moment object into a date object:
From http://momentjs.com/docs/#/displaying/as-javascript-date/
moment().toDate();
Yields:
Tue Nov 04 2014 14:04:01 GMT-0600 (CST)
Moment.js - How to convert date string into date?
If you are getting a JS based date String
then first use the new Date(String)
constructor and then pass the Date
object to the moment
method. Like:
var dateString = 'Thu Jul 15 2016 19:31:44 GMT+0200 (CEST)';
var dateObj = new Date(dateString);
var momentObj = moment(dateObj);
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
In case dateString
is 15-07-2016
, then you should use the moment(date:String, format:String)
method
var dateString = '07-15-2016';
var momentObj = moment(dateString, 'MM-DD-YYYY');
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
Parse string to date with moment.js
You need to use the .format()
function.
MM
- Month number
MMM
- Month word
var date = moment("2014-02-27T10:00:00").format('DD-MM-YYYY');
var dateMonthAsWord = moment("2014-02-27T10:00:00").format('DD-MMM-YYYY');
FIDDLE
How to convert moment date to a string and remove the moment object
Use moment().format()
to create a formatted string from the date.
console.log(moment().format())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
Moment JS convert friendly date to JS date
You need to "tell" moment from which format you want to create a date (second argument).
const exampleDate = '1st January 2019'
moment(exampleDate, 'Do MMMM YYYY').format('DD-MM-YYYY');
Link to JS Fiddle
moment js to convert date from one format to other
From moment docs:
By default, two digit years above 68 are assumed to be in the 1900's and years 68 or below are assumed to be in the 2000's. This can be changed by replacing the
moment.parseTwoDigitYear
method.
So if you replace moment.parseTwoDigitYear
in you code, you will have 2069 instead of 1969.
Here a working example:
moment.parseTwoDigitYear = function(year){ return parseInt(year, 10) + 2000;};
var s1 = moment("23MAY68", 'DDMMMYY').format('YYYY-MM-DD');var s2 = moment("23MAY99", 'DDMMMYY').format('YYYY-MM-DD');
console.log(s1, s2);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
Related Topics
Jquery Function Not Binding to Newly Added Dom Elements
Password-Check Directive in Angularjs
How to Check If Element Has Any Children in JavaScript
How to Update React Context from Inside a Child Component
Script Tag in Angular2 Template/Hook When Template Dom Is Loaded
How to Import a CSS File in a React Component
How to Make a JSON Call to an Url
Reason Behind This Self Invoking Anonymous Function Variant
Is There a JavaScript MVC (Micro-)Framework
How to Listen for a Click-And-Hold in Jquery
How to Update States 'Onchange' in an Array of Object in React Hooks
How to Reverse an Array in JavaScript Without Using Libraries
Detect If Browser Tab Is Active or User Has Switched Away
Watch for Object Properties Changes in JavaScript
Chrome Extension: Make It Run Every Page Load
React Usestate Hook Event Handler Using Initial State