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

80
Views
Validating JavaScript datetimeZone format

I can validate the format of this dates using moment library:

2022-03-26T14:15:51 ("YYYY-MM-DDTHH:mm:ss")
2022-03-26 ("YYYY-MM-DD")

But having problem with following :

2022-03-26T14:15:51.667778+03:00  ("YYYY-MM-DDTHH:mm:ss.SSSZ") - this format not solving the problem.

Is there any solution? Maybe using regex will solve?

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

0

You could try the format YYYY-MM-DDTHH:mm:ss.SSSSSSZ, I'd also suggest using strict mode when parsing dates. This format will allow a variable amount of digits after the decimal point but will refuse to parse a date with no fractional seconds:

function validateDate(input, format) {
    const dt = moment(input, format, true);
    return dt.isValid();
}

const testInputs = [
    '2022-03-26T14:15:51.667778+03:00',
    '2022-03-26T14:15:51.667+03:00',
    '2022-03-26T14:15:51+03:00',
    '2022-03-26T14:15:51',
    'FOO'
]

const format = 'YYYY-MM-DDTHH:mm:ss.SSSSSSZ';

console.log('Input'.padEnd(36), 'Valid');
testInputs.forEach(input => console.log(input.padEnd(36), validateDate(input, format)))
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>

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!