I completely agree with @Adrian Brand. The issue at hand is not specific to Angular but rather how date-time handling is managed within your application.
Based on my personal experience, working with date-time functionalities using the built-in JavaScript Date object can be quite challenging.
Furthermore, when dealing with Pacific Time, one must also consider addressing daylight saving time changes, which adds another layer of complexity to the scenario.
The Pacific Time Zone (PT) covers regions in western Canada, western United States, and western Mexico. This zone follows standard time by subtracting eight hours from Coordinated Universal Time (UTC−08:00). During daylight saving time, a time offset of UTC−07:00 is observed.
Assuming you are still sending data in ISO format to the server.
For instance, being located in Singapore would yield the below results for a normal Date constructor output.
var date = new Date(2019, 3, 5)
. //Fri Apr 05 2019 00:00:00 GMT+0800 (Singapore Standard Time)
date.toISOString()
//2019-04-04T16:00:00.000Z
. Due to Singapore being in +08:00, which is 8 hours ahead of UTC.
If aiming to consistently obtain PST time output from new Date, it requires...
- Overriding the Date constructor, although this is not recommended.
- Implementing a new method within the Date construction for precise ISO formatting that caters to the PST Time zone. This entails string replacements and calculating offsets through mathematical operations.
Including a Method to Display Strings in PST
By utilizing an exact date parameter in the constructor like var date = new Date(2019, 3, 5)
, appending a method call such as toPSTString()
, where regex functions replace text inside parentheses with "Pacific Standard Time" and convert GMT+xxx
to
GMT-08:00</code given the absolute value of the date.</p>
<pre class="lang-js"><code>Date.prototype.toPSTString = function () {
let date = this.toString();
//date.replace.... easy part, you could use Regex to do it
return date;
};
However, situations involving ISO String-formatted dates or milliseconds parameters make the process more intricate. For example, if initializing
new Date("2019-04-05T07:00:00.000Z")
, desired outputs need careful consideration.
Below demonstrates how I adjust ISO String output based on offset differences.
Creating a Method for ISO String Representation Considering PST -08:00 Time Zone
new Date
typically reflects local machine timezone settings. Therefore, running
new Date(2019, 3, 5).toISOString()
in Singapore would yield
2019-04-04T16:00:00.000Z
, not the expected
2019-04-05T08:00:00.000Z</code in PST.</p>
<p>A potential solution involves modifying JS functions to display UTC dates while accounting for the PST timezone.</p>
<pre class="lang-js"><code>Date.prototype.toPSTString = function () {
function convertMinuteToMillisecond(mins) {
return mins * 60 * 1000;
}
let localDateOffsetToUtc = this.getTimezoneOffset();
const offSetBetweenPSTAndUTC = 480;
let offsetBetweenPSTAndLocal = offSetBetweenPSTAndUTC - localDateOffsetToUtc;
let newDate = new Date(
this.getTime() + convertMinuteToMillisecond(offsetBetweenPSTAndLocal)
);
return newDate.toISOString();
};
var date = new Date(2019, 3, 5);
date.toISOString(); //"2019-04-04T16:00:00.000Z" Singapore
date.toPSTString(); //"2019-04-05T08:00:00.000Z" PST
This presentation seems accurate. While untested, I believe it provides insight into resolving the issue.
In reality, users in Singapore desire viewing dates in their own timezone rather than PST. Similarly, individuals in London prefer their local time over Singapore or PST zones. Consider these factors carefully to prevent escalating complexities within your application.
To delve deeper into managing timezone and locale concerns, visit my blog post where I discuss how I handle these issues. Personally, I utilize moment.js and rely on server-side support. Feel free to explore for additional insights.