javascript - parse date-fns returns one day previous value - Stack Overflow

I am trying to parse date using date-fns library for the first time (I removed moment.js and I will int

I am trying to parse date using date-fns library for the first time (I removed moment.js and I will introduce date-fns in my project) but, when I use parse function, I get one day previous as result. I read in this topic

parse function in date-fns returns one day previous value

that problem is due to timezones but I could not be able to fix my problem because I receive dates in the format "yyyyMMdd". This is my code

var start ="20210119";
var stop ="20210130";

const dateFrom = parse(start, "yyyyMMdd", new Date());
console.log("date form",dateFrom);

Output of my console log is: 2021-01-18T23:00:00.000Z

I am trying to parse date using date-fns library for the first time (I removed moment.js and I will introduce date-fns in my project) but, when I use parse function, I get one day previous as result. I read in this topic

parse function in date-fns returns one day previous value

that problem is due to timezones but I could not be able to fix my problem because I receive dates in the format "yyyyMMdd". This is my code

var start ="20210119";
var stop ="20210130";

const dateFrom = parse(start, "yyyyMMdd", new Date());
console.log("date form",dateFrom);

Output of my console log is: 2021-01-18T23:00:00.000Z

Share Improve this question asked Sep 13, 2021 at 9:05 P_RP_R 4882 gold badges8 silver badges30 bronze badges 2
  • 1 It's unclear what you want. Do you want to parse yyyyMMdd format timestamps as UTC? By default, if no timezone is specified, timestamps will be parsed as local, which is why you see a difference between the initial timestamp and generated timestamp—the original is parsed as local but logged as UTC. – RobG Commented Sep 13, 2021 at 10:20
  • 1 Probably a duplicate of Proper way to parse a date as UTC using date-fns. – RobG Commented Sep 13, 2021 at 10:28
Add a ment  | 

1 Answer 1

Reset to default 5

Your dateFrom date is actually correct, assuming you want it in your local time.

The reason it's showing an hour earlier in your console output is because it is being displayed in UTC time (hence the 'Z' for Zulu at the end). I assume your local time is one hour ahead of UTC in January.

We can use Date.toLocaleString() to output the time in either UTC or local time.

If we output in local time, we see the date is actually correct (2021-01-19 00:00:00), and if we output in UTC (set the timeZone to 'UTC') we see it is one hour earlier (as in your console output).

We can also output the local date using Date.getFullYear(), Date.getMonth() and Date.getDate().

var start ="20210119";
var stop ="20210130";

const dateFrom = dateFns.parse(start, "yyyyMMdd", new Date());

console.log("Date from (is Date object):", dateFrom instanceof Date);

console.log("\nDate from (console.log):",dateFrom);
console.log("Date from (UTC):", dateFrom.toLocaleString([], { timeZone: 'UTC' }));

   
console.log("\nDate from (Local):", dateFrom.toLocaleString());
console.log("Date from (Local):", dateFrom.toDateString());
console.log("Date from (Local):", `${dateFrom.getFullYear()}-${dateFrom.getMonth()+1}-${dateFrom.getDate()}`);
<script src="https://cdnjs.cloudflare./ajax/libs/date-fns/1.28.5/date_fns.min.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790758a4593899.html

相关推荐

  • javascript - parse date-fns returns one day previous value - Stack Overflow

    I am trying to parse date using date-fns library for the first time (I removed moment.js and I will int

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信