Base solution for your next web application
Open Closed

Question about DateTimeService #10773


User avatar
0
cangunaydin created

Prerequisites

Please answer the following questions before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • What is your product version? v11 prerelease
  • What is your product type (Angular or MVC)? Angular
  • What is product framework type (.net framework or .net core)? .net 6

If issue related with ABP Framework

  • What is ABP Framework version? latest

Hello, I have upgraded my project to v11 from v9.3 so i can see that moment library has changed to luxon and there is datetimeservice which do the date manipulation and formatting. So while i was inspecting the code i have found sth.

 createUtcDate(year: number, month: number, day: number): DateTime {
        return DateTime.utc(year, month + 1, day);
    }

    toUtcDate(date: DateTime | Date): DateTime {
        if (date instanceof Date) {
            return this.createUtcDate(date.getFullYear(), date.getMonth(), date.getDate());
        }

        return this.createUtcDate(date.year, date.month, date.day);
    }

here is the code block from DateTimeService, I have 2 questions about this code block

  1. as you can see over here if toUtcDate gets javascript date instance, then it sends the month directly from javascript date object which starts from 0 index. Since luxon months starts from index 1, it is added +1 over there and we get the correct result. On the other hand if toUtcDate gets DateTime object i think this code will break since date.month in luxon starting from 1, which means createutcdate shouldn't add +1 to the month. I believe this is a bug, so i have changed the code to
    createUtcDate(year: number, month: number, day: number): DateTime {
        return DateTime.utc(year, month, day);
    }
  //change the timezone to utc with same values from local, keep local time values
    toUtcDate(date: DateTime | Date): DateTime {
        if (date instanceof Date) {
            return this.createUtcDate(date.getFullYear(), date.getMonth() + 1, date.getDate());
        }

        return this.createUtcDate(date.year, date.month, date.day);
    }
  1. since createUtcDate() method doesn't take times isn't it better to use sth like date.setZone("utc", { keepLocalTime: true }); i think if you are manipulating datetime objects it is better to use this version? maybe for js instances you can not do that? https://moment.github.io/luxon/#/zones?id=keeplocaltime

Looking forward for your reply, thanks again.


3 Answer(s)