
In this article, we’ll see some tricky scenarios and will be solving them with modern techniques.
1. padStart and padEnd
If you have to show numbers in some standard or minimum digit count, you can use padStart or padEnd.
E.g If you’ve to show days in 2 digit count like 09 Days. So this was the old technique we use to solve this case.
1. Getting Current UTCTime String:
2. Converting device local time to UTC time string:
If the date is a string, and in the format "2021–10–22" (yyyy-MM-dd), convert it to a standard time string. For example, to "2020–10–21T19:00:00.000Z"
Then use the following method to convert this standard date string to UTC date-time string:
3. Converting UTC date-time string to device’s local time:
Make sure your UTC date-time string is in the standard format, for example "2021–10–22T05:00:00.000Z"
Then use the following method to convert this date string into the device’s local date:
Summary:
Sometimes the Date object is hard to work with. For example, some libraries do not work as expected. Thus, using these functions will be a lifesaver. They definitely helped me with a recent project. And I thought about sharing my experience with you.