Parse and format date in string

Fellow Stranger

I'm trying out date-fns v2.

I want to format a date in a string by using the toDate and format functions:

import { format, toDate } from 'date-fns'
format(toDate('2019-02-11T14:00:00'), 'MM/dd/yyyy')

But get the following error:

RangeError: Invalid time value

NineBerry

It seems that you are using Version 2.0 of date-fns, which is still in alpha (development) status currently.

What functions are available and how they work still seems to change frequently. For example, while in version v2.0.0-alpha.26 toDate() can handle string parameters, it cannot do that any longer in version v2.0.0-alpha.27. There is a new parseISO() function instead.

This should work now:

format(parseISO('2019-02-11T14:00:00'), 'MM/dd/yyyy')

However, while version 2 is still in beta, I would suggest using the stable version 1.x for now.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Parse date string and change format

Parse String date to specific format

Unable to parse the string to date format

How to parse JSON format date string into date format

DateFormatter parse String to Date gives wrong format

Parse some string date to another format

Parse String to Date with Different Format in Java

Parse a date/time string in specific format

Parse UTC date string and convert to different format

How to parse a date to get the string in a particular format

Correct format string to parse a date time in Haskell

Parse Date String to UTC UNIX Format

Parse date string to a specific format in Java

Time parse error in Golang (parse string to date format)

Java: Parse multiple String Date Time format into LocalDateTime/Date/Calender

Java8 parse date or date time format for a given string

How to parse a string to date and back to string with same format

Parse Date from string present in multiple formats into datetime format

Parse 'Date & Time' string in Javascript which are of custom format

Parse JSON date format to String using GSON on Android

FileHelpers will only parse datetime if date string matches universal time format

Parse string to get date and time in a specific format in Clojure

ParseExact cannot parse a string in RFC 3339 Internet Date/Time format

Parse date in String format to Gregorian Calendar, with hour and minute

Parse and format custom date string Moment.js

Parse the String column to get the data in date format using Spark Scala

SimpleDateFormat parse and format on same date string gives different results

How to change date format to parse into url_string?

Parse and format Date in Android