Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js - How to convert date string into date?

Following up from my previous post: Javascript Safari: new Date() with strings returns invalid date when typed

I am using Moment.js to convert a date string into a date field based on user input in the text box.

This is to prevent the problem I described in the linked post for Safari and Firefox not able to render the date when Chrome is fine.

Here is the code snipper:

var tempDate = moment(userInputFieldDate).format('DD-MM-YYYY'); alert(tempDate); 

In Chrome, it does work fine (it use to work with the Javascript Date object too) but gives me the moment.js deprecation warning

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info. Arguments: [object Object] Error

On Firefox and Safari is just gives an UNDEFINED DATE in the alert window. So not entirely sure what should I be doing to convert the date string to a Date object.

Any suggestions on this issue?

like image 998
noobcoder Avatar asked Jul 07 '16 17:07

noobcoder


People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

How do you convert a string to a date in JavaScript?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

How do you turn a moment into a date?

If you are creating timestamps using built-in new Date() , you can also use it to create moment objects: const timestamp = new Date(); const momentTimestamp = moment(timestamp); If you want to create a moment object using current date and time, just call moment() without any arguments.

How do you convert a string to a moment object?

We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console.


1 Answers

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 
like image 65
Matyas Avatar answered Oct 10 '22 01:10

Matyas