I am facing an issue while writing a function with [dayjs][1]
library.
The problem I am trying to solve, is to extract the date part (only) from a given date-time string and ensure that the value of date is not changed.
For example: "Thu Jan 30 2225 23:45:46 GMT-0800 (Pacific Standard Time)"
should result in "2225-01-30"
in YYYY-MM-DD
format, but with the solution I attempted (code snippet below), I am getting output as "2225-01-31"
.
In summary, I do not need any time-conversion to be applied, while extracting the date part. I have also tried to include dayjs/plugin/timezone
but the date change happens before itself, when dayjs(date)
is invoked.
Code snippet:
const date = "Thu Jan 30 2225 23:45:46 GMT-0800 (Pacific Standard Time)";
const formattedDate = dayjs(date).format("YYYY-MM-DD");
console.log(formattedDate);
<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
Any insights on this will be helpful. I am open to writing a native JavaScript function as well or consider other libraries.
I have also gone through similar question asked before, and have tried the suggestions and still not getting the desired outcome.