Utilize string parsing or leverage a C# library
I discovered two methods to extract the necessary data, however, both approaches are somewhat crude and inelegant.
- To extract the timezone name on the front end, one can utilize the
.toString()
function from the moment library and then employ pattern matching techniques to isolate the timezone name from the result.
This process involves using regex or .split()
to separate the timezone name from the date string, assuming the format remains consistent. An example implementation looks like:
var timeZoneName = momentDate._d.toString().split("(")[1].split(")")[0];
//timeZoneName now stores "Pacific Daylight Time"
After transferring this value to the back end, it is necessary to replace "Daylight" with "Standard" in order to create a TimeZoneInfo instance.
- Alternatively, on the back end, utilize an external library to handle the mapping process.
I came across two libraries, TimeZoneNames and TimeZoneConverter, which can convert IANA-style names to their Windows Standard equivalents. The TimeZoneConverter can be used as follows:
localTZName = TZConvert.IanaToWindows(tzName);
TimeZoneInfo localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(localTZName);
The tzName is obtained by using moment.tz.guess()
on the front end. While this method proved effective, it essentially involves creating a mapping between the two styles, alleviating the need for manual conversion.
Although both solutions are not ideal, they serve their purpose
The first solution relies on the assumption that the moment's string representation remains consistent, prompting the need for a more streamlined method to extract this specific data without resorting to string manipulation.
The second solution involves a direct mapping approach, which may not be aesthetically pleasing but does the job effectively. Ideally, this functionality should be integrated seamlessly into either moment.tz.js or TimeZoneInfo.
The discrepancy arises from moment.js disregarding Windows compatibility, while Windows overlooks the intricate nature of timezones and their historical implications on accurate date representation.