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!
try this
var newStringDate = yourDate.toISOString().slice(0, 10);
or
var newStringDate = yourDate.toISOString().substring(0, 10);
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>