As you can see, with moment lib, we need 2 steps to get the result: parse string to Date object, then format date object to string.

Your code - format(new Date("10-13-20"), 'MM-DD-YY') is format step, try convert a date object to a string with format template is MM-DD-YY. But your date object is not correct.

The solution is to do the same as with moment lib:

  1. Parse date string to date object. Use parse

    const dateString = '10-13-20';
    const date = parse(dateString, 'MM-dd-yy', new Date()) // not MM-DD-YY
    
  2. Format date object to result string. Use format

    const result = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
    console.log(result)
    

    Result will be like (the same with moment's result in my timezone):

     2020-10-13T00:00:00.000+09:00
    
Answer from hoangdv on Stack Overflow
🌐
date-fns
date-fns.org › docs › format
date-fns - modern JavaScript date utility library
date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
🌐
Envato Tuts+
code.tutsplus.com › home › javascript
Using date-fns for Easy Date Manipulation | Envato Tuts+
July 28, 2023 - We then use the format() function to format the date according to the provided format string. The format string uses placeholders such as dd for a two-digit day, MMMM for the full month name, and yyyy for the full year.
🌐
Handsontable
handsontable.com › home › hands-on: format date with date-fns and day.js in a data grid
Hands-on: Format date with date-fns and day.js in a data grid
August 25, 2023 - import dayjs from 'dayjs'; import customParseFormat from 'dayjs/plugin/advancedFormat'; dayjs.extend(customParseFormat); export default class FormatWithDayJs { // Format date as Age static formatDateToAge(dateString) { const birthDate = dayjs(dateString, 'YYYY-MM-DD'); const age = dayjs().diff(birthDate, 'year'); return age; }; // Format date as Day of Birth (e.g., Monday, Tuesday, etc.) static formatDateToDayOfBirth(dateString) { const birthDate = dayjs(dateString, 'YYYY-MM-DD'); return birthDate.format('dddd'); }; // Format date as Month of Birth (e.g., January, February, etc.) static format
🌐
npm
npmjs.com › package › date-fns
date-fns - npm
import { compareAsc, format } from "date-fns"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '2014-02-11' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] The library is available as an npm package.
      » npm install date-fns
    
Published   Sep 17, 2024
Version   4.1.0
🌐
Reddit
reddit.com › r/webdev › date-fns thinks format dd/mm/yyyy hh:mm:ss is an invalid time value
r/webdev on Reddit: Date-fns thinks format dd/MM/yyyy HH:MM:SS is an invalid time value
April 15, 2021 -

Started getting an issue when processing DateTime strings from a json array. I am reading in values from a list, specifically DateTime strings from a variable called record_date.

This string comes pre-formatted from another application, usually excel csv files that have been converted into json. I am attempting to convert this DateTime string into the "pp" format supplied by date-fns (12:00:00 AM).

So for instance, If I have a DateTime string that says 15/04/2021 11:28:00. I would like to format this as simple 11:28:00 AM, but I am getting an error that says "Invalid time value". I'm not really sure what is wrong with my code as I do all of my processing on a single line. I have tried processing it using a couple different methods, but they aren't working. Maybe someone here knows what's going wrong?My code;

item.record_date = format(new Date(parseISO(item.record_date)), "pp");

have also tried this;

item.record_date = format(parseISO(item.record_date), "pp");

and also this;

item.record_date = format(new Date(item.record_date), "pp");
🌐
date-fns
date-fns.org › v1.29.0 › docs › format
date-fns format
var result = format( new Date(2014, 1, 11), 'MM/DD/YYYY' ) //=> '02/11/2014' Represent 2 July 2014 in Esperanto: var eoLocale = require('date-fns/locale/eo') var result = format( new Date(2014, 6, 2), 'Do [de] MMMM YYYY', {locale: eoLocale} ) //=> '2-a de julio 2014' Suggest edits by sending a PR ·
🌐
GitHub
github.com › date-fns › date-fns › issues › 1998
How do i validate for dd/mm/yyyy, dd-mm-yyyy, yyyy/mm/dd and yyyy-mm-dd formats? · Issue #1998 · date-fns/date-fns
September 29, 2020 - I must validate a string that is supposed to follow dd/mm/yyyy, dd-mm-yyyy, yyyy/mm/dd or yyyy-mm-dd formats. Can i do it with date-fns or i'll have to consider using regex?
Published   Sep 29, 2020
Find elsewhere
🌐
Tabnine
tabnine.com › home page › code › javascript › date-fns
date-fns.format JavaScript and Node.js code examples | Tabnine
const exportConfig = async () => { try { const chosenFolders = await dialog.showOpenDialog(mainWindow, { properties: ['openDirectory'] }); if (chosenFolders && chosenFolders.canceled === false) { const date = format(new Date(), 'MM-DD-YYYY HH[:]mm'...
🌐
DEV Community
dev.to › marinamosti › -formatting-and-modifying-javascript-dates-with-date-fns-3df2
Formatting and modifying JavaScript dates with date-fns - DEV Community
September 23, 2019 - const newYears = new Date('2019/01/01'); const formattedDate = format(newYears, 'MM/DD/YYYY'); console.log(formattedDate); If you run this and check out your console output, you'll see that you are getting back your formatted string. 01/01/2019 · Wait, that's it? Yup! Date-fns makes formatting super simple!
🌐
GitHub
github.com › date-fns › date-fns › issues › 942
parse('2-12-12', 'yyyy-MM-dd', new Date()) returns a valid date · Issue #942 · date-fns/date-fns
October 19, 2018 - Is this intended? I would expect parse('2-12-12', 'yyyy-MM-dd', new Date()) to fail as the correct format would be '0002-12-12'.
Published   Oct 19, 2018
🌐
Nuxt
nuxt.com › modules › date-fns
@nuxtjs/date-fns · Nuxt Modules
For typescript to be aware of the additions to the nuxt Context, the vue instance and the vuex store, the types need to be merged via declaration merging by adding @nuxtjs/date-fns to your types. ... Locales to be imported. ... You can preset default locale. ... You can preset a fallback locale for when a method is called with an unsupported locale. ... You can preset default format. ... Methods to be imported. If not defined all methods are imported. ... <template> <div> // Using default format and locale {{ $dateFns.format(new Date()) }} // Using custom format {{ $dateFns.format(new Date(), 'yyyy-MM-dd') }} // Using custom format and locale {{ $dateFns.format(new Date(), 'yyyy-MM-dd', { locale: 'ru' }) }} // Using asyncData {{ dateFormatted }} </div> </template> <script> export default { asyncData({ $dateFns }) { return { dateFormatted: $dateFns.format(new Date()) } } } </script>
🌐
GitHub
github.com › date-fns › date-fns › issues › 1816
dateFns.format() returning incorrect date. · Issue #1816 · date-fns/date-fns
May 31, 2020 - dateFns.format(info.event.start, 'MMM d, YYYY') // Returns Incorrect Date dateFns.format(info.event.end, 'MMM d, YYYY') // Returns Correct Date
Published   May 31, 2020
🌐
npm
npmjs.com › package › date-fns-tz
date-fns-tz - npm
import { format, toZonedTime } from 'date-fns-tz' const date = new Date('2014-10-25T10:46:20Z') const nyDate = toZonedTime(date, 'America/New_York') const parisDate = toZonedTime(date, 'Europe/Paris') format(nyDate, 'yyyy-MM-dd HH:mm:ssXXX', { ...
      » npm install date-fns-tz
    
Published   Sep 30, 2024
Version   3.2.0
Author   Marnus Weststrate
🌐
Medium
medium.com › @mikiasoliveira › formatando-e-modificando-datas-em-javascript-com-date-fns-438c75ed87e9
Formatando e modificando datas em JavaScript com date-fns | by Mikéias Oliveira | Medium
June 15, 2019 - const newYears = new Date(‘2019/01/01’); const formattedDate = format(newYears, ‘MM/DD/YYYY’); console.log(formattedDate); Se você executar isso e verificar a saída no console, verá que está recebendo sua string formatada: 01/01/2019. Espere, é isso? Sim! Date-fns torna a formatação super simples!
🌐
GitHub
github.com › date-fns › date-fns › issues › 720
Parse does not correctly handle DD/MM/YYYY format · Issue #720 · date-fns/date-fns
January 14, 2018 - Ran in the dev console on https://date-fns.org/v2.0.0-alpha.7/docs/parse in Chrome 65 and Safari 11: > dateFns.parse('02/11/2014', 'MM/DD/YYYY', new Date()) Tue Feb 11 2014 00:00:00 GMT+0000 (GMT) > dateFns.parse('02/11/2014', 'DD/MM/YYYY', new Date()) Tue Feb 11 2014 00:00:00 GMT+0000 (GMT) Second one should be Sun Nov 02 2014 00:00:00 GMT+0000 (GMT).
Published   Mar 28, 2018