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

185
Views
how do I get only the date from the string "2022-05-25T15:48:50.000Z" using moment.js?

I am using moment.js library and for the example lets say I have the code -

import moment from "moment-timezone";

const date = "2022-05-25T15:48:50.000Z"

I've tried using moment(date).format("YYYY-MM-DD") but I am getting the previous date - 2022-05-24 as a result.

I know it has to do with utc, and my project has in it's setting the line 'moment.tz.setDefault("Etc/UTC")` which from my understand is supposed to fix it, but it doesn't.

I only need it to have the final result of 2022-05-25 and I know I am missing something really small here..

I'd appreciate any help!

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

0

try this var newStringDate = yourDate.toISOString().slice(0, 10); or

var newStringDate = yourDate.toISOString().substring(0, 10);
about 4 years ago · Juan Pablo Isaza Report

0

Your format is ISO8601 format, you could check from here: https://en.wikipedia.org/wiki/ISO_8601

You can try to send the full format to the function

const date = "2022-05-25T15:48:50.000Z"
var now = moment.utc(date,"YYYY-MM-DD\THH:mm:ss\Z").format("YYYY-MM-DD");
document.getElementById('now').innerHTML = now;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<strong>Should Show: 2022-05-25</strong>
<div id="now"></div>

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!