Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Moment format returning 'invalid date'

I have the following:

const input: string = '2021-11-2T10:20:00+01:00';

const date: string = moment( input ).format('YYYY-MM-DD');
const time: string = moment( input ).format('HH:mm');

Please can someone tell me why I get 'invalid date' returned for both 'date' and 'time'?

From what I understand the 'input' string is a standard date format (moment gave me this format).

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Take care about leading zeros (required by RFC2822/ISO format). You can validate date string using Date constructor:

new Date('2021-11-2T10:20:00+01:00') // Invalid Date (because of date = '2' instead of '02')
new Date('2021-11-02T10:20:00+01:00') // Tue Nov 02 2021 11:20:00 GMT+020 (works fine!)

Same for moment instances:

moment('2021-11-2T10:20:00+01:00') // invalid (error or deprecation warning depends on 'moment' version)
moment('2021-11-02T10:20:00+01:00') // valid

UPD: article about standarts with useful links

about 4 years ago · Juan Pablo Isaza Report

0

What you are using is known as timestamptz. There's an error. So timestamptz is in the following format:

YYYY-MM-DDThh:mm:ssZZ

You are missing a "0" in your day:

2021-11-02T10:20:00+01:00

Then you can just do:

const fullDate = moment('2021-11-02T10:20:00+01:00')
const date = moment( input ).format('YYYY-MM-DD')
const time = moment( input ).format('HH:mm')

Notice i removed : string. You don't need to define the typing btw. It can be infered.

about 4 years ago · Juan Pablo Isaza Report

0

The input string I was using was somehow not valid. When I used "2021-11-02T14:43:59+00:00" it works fine.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!