PoskOfficial / Nepali-calendar-web

A PWA for minimal Nepali Calendar
https://miti.bikram.io/
BSD 2-Clause "Simplified" License
86 stars 22 forks source link

Date display inconsistency in input field and paragraph. #56

Closed pandeysubash404 closed 1 year ago

pandeysubash404 commented 1 year ago

53

Problem Description: When using the provided input field, the displayed date appears to be one day behind the actual date. However, the date displayed in the paragraph below the input field shows the correct date. This inconsistency can be confusing for users, leading to potential misunderstandings or incorrect data inputs.

Cause of the Issue: The issue is caused by the difference in how the date is handled between the input field and the paragraph. The input field utilizes the toISOString() method to convert the date to an ISO string, which represents the date and time in Coordinated Universal Time (UTC). The string is then split to extract only the date portion. Conversely, the paragraph utilizes the toLocaleString()method, which considers the local time zone of the user's browser.

Due to this discrepancy, when the input field displays the date in UTC and the user is in a time zone with a positive offset from UTC, such as UTC+01:00, the date appears to be one day behind the user's local date.

Proposed Solution: To address this issue and ensure consistent date representation across different time zones, I suggest the following solution:

  1. Adjust the code responsible for assigning the value to the input field as follows:

value={new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().split("T")[0]}

  1. Explanation of the code adjustment:
    • date.getTimezoneOffset() retrieves the offset in minutes between the user's local time zone and UTC.
    • date.getTime() - date.getTimezoneOffset() * 60000 calculates the adjusted time in milliseconds, taking into account the user's time zone offset.
    • new Date(...).toISOString().split("T")[0] converts the adjusted time to an ISO string and extracts the date portion.

By implementing this solution, the input field will display the correct date according to the user's local time zone, aligning it with the date displayed in the paragraph.

Screenshots Here are some of the screenshots that might be useful to understand the problem : Date Converter screen :

Miti

I hope this report helps you in resolving the date display discrepancy. If you have any further questions or require additional assistance, please feel free to reach out. Thank you for your attention to this matter.

Parajulibkrm commented 1 year ago

Can you create a PR for this?

Parajulibkrm commented 1 year ago

Duplicate of #53

Parajulibkrm commented 1 year ago

Could you please include the pnpm-lock (without pnpm as dependency) and remove the package-lock, sorry for the confusion.

pandeysubash404 commented 1 year ago

Hi @Parajulibkrm ,

I have made the requested changes to the repository as per your instructions.

I have also installed pnpm globally using npm, so the project can now be installed and run with pnpm.

Please review the changes and let me know if everything looks good. If you have any further instructions or suggestions, please feel free to let me know.

Thank you for your guidance, and I look forward to your feedback.

Best regards, Subash Pandey

Parajulibkrm commented 1 year ago

Fixed by #57