如何将日(星期几)和时间作为字符串转换为.Net中的不同时区

时间:2022-10-21 10:53:04

I have to convert a various amount of strings in to the local timezone (.ToLocalTime()) but unlike a normal DateTime string those strings only contain day and time (they are supposed to be in UTC) like so:

我必须将不同数量的字符串转换为本地时区(.ToLocalTime()),但与普通的DateTime字符串不同,这些字符串只包含日期和时间(它们应该是UTC格式),如下所示:

SUNDAY:17:00 
FRIDAY:16:50
MONDAY:20:50
THURSDAY:07:00

I am looking for a clever way to "convert" those strings to the local Timezone but since I can not get a DateTime out of this I would have to do it manually which I would like to avoid. (also I would not like to split the string... )

我正在寻找一种聪明的方法将这些字符串“转换”到本地时区,但由于我无法获得DateTime,我必须手动完成,我想避免。 (我也不想拆分字符串......)

2 个解决方案

#1


1  

You cannot handle TimeZoneOffset correctly without knowing the exact date, because the offset may change from one day to the other (DST vs non-DST).

如果不知道确切的日期,则无法正确处理TimeZoneOffset,因为偏移量可能会从一天变为另一天(DST与非DST)。

Getting the correct time is the easy part (if you have the correct date), just use the appropriate constructor of DateTime.

获取正确的时间是很容易的部分(如果你有正确的日期),只需使用DateTime的相应构造函数。

DateTime(int year, int month, int day, int hour, int minute, int second, System.DateTimeKind kind)

and specify the kind as System.DateTimeKind.Utc. Then you have your timestamp as a UTC time and you can then use ToLocalTime() to get the correct time in your local timezone.

并将类型指定为System.DateTimeKind.Utc。然后,您将时间戳作为UTC时间,然后可以使用ToLocalTime()在本地时区获取正确的时间。

The hard part seems to me, is getting the correct date out of your data. Which SUNDAY is meant, the next one (ie 2016-07-10) or the last one (2016-07-03)? This is especially important for dates near the switch of DST because this may result in different timestamps.

在我看来,困难的部分是从数据中获取正确的日期。哪个星期日是指,下一个(即2016-07-10)或最后一个(2016-07-03)?这对于DST切换附近的日期尤为重要,因为这可能会导致不同的时间戳。

Also be aware that 'SUNDAY:23:00' UTC may become 'MONDAY:01:00' (and vice versa) for some timezones! So you'll have to do the calculation of the date also based on UTC and not your local time. For instance, if in local time it's Monday 2016-07-04 01:00, but this is Sunday 2016-07-03 23:00 in UTC then interpreting a timestamp SUNDAY:17:00 using the local time (and wanting the next sunday) will result in 2016-07-10 19:00 instead of 2016-07-03 19:00.

另请注意,“星期日:23:00”UTC的某些时区可能会变成“星期一:01:00”(反之亦然)!因此,您还必须根据UTC而不是当地时间计算日期。例如,如果在当地时间是星期一2016-07-04 01:00,但是这是2016年6月17日星期日UTC,那么使用当地时间解释时间戳SUNDAY:17:00(并希望下一个星期日)将导致2016-07-10 19:00而不是2016-07-03 19:00。

#2


1  

You could use the DateTime Constructor to create a date and convert it like a normal DateTime, no?

您可以使用DateTime构造函数创建日期并将其转换为普通的DateTime,不是吗?

Check this example, and replace with what you need.

检查此示例,并替换为您需要的内容。

var theDate = new DateTime (DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, hours, minute, second);

From the doc:

从文档:

public DateTime(
    int year,
    int month,
    int day
)

DateTime date1 = new DateTime(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
//      8/18/2010 12:00:00 AM   

#1


1  

You cannot handle TimeZoneOffset correctly without knowing the exact date, because the offset may change from one day to the other (DST vs non-DST).

如果不知道确切的日期,则无法正确处理TimeZoneOffset,因为偏移量可能会从一天变为另一天(DST与非DST)。

Getting the correct time is the easy part (if you have the correct date), just use the appropriate constructor of DateTime.

获取正确的时间是很容易的部分(如果你有正确的日期),只需使用DateTime的相应构造函数。

DateTime(int year, int month, int day, int hour, int minute, int second, System.DateTimeKind kind)

and specify the kind as System.DateTimeKind.Utc. Then you have your timestamp as a UTC time and you can then use ToLocalTime() to get the correct time in your local timezone.

并将类型指定为System.DateTimeKind.Utc。然后,您将时间戳作为UTC时间,然后可以使用ToLocalTime()在本地时区获取正确的时间。

The hard part seems to me, is getting the correct date out of your data. Which SUNDAY is meant, the next one (ie 2016-07-10) or the last one (2016-07-03)? This is especially important for dates near the switch of DST because this may result in different timestamps.

在我看来,困难的部分是从数据中获取正确的日期。哪个星期日是指,下一个(即2016-07-10)或最后一个(2016-07-03)?这对于DST切换附近的日期尤为重要,因为这可能会导致不同的时间戳。

Also be aware that 'SUNDAY:23:00' UTC may become 'MONDAY:01:00' (and vice versa) for some timezones! So you'll have to do the calculation of the date also based on UTC and not your local time. For instance, if in local time it's Monday 2016-07-04 01:00, but this is Sunday 2016-07-03 23:00 in UTC then interpreting a timestamp SUNDAY:17:00 using the local time (and wanting the next sunday) will result in 2016-07-10 19:00 instead of 2016-07-03 19:00.

另请注意,“星期日:23:00”UTC的某些时区可能会变成“星期一:01:00”(反之亦然)!因此,您还必须根据UTC而不是当地时间计算日期。例如,如果在当地时间是星期一2016-07-04 01:00,但是这是2016年6月17日星期日UTC,那么使用当地时间解释时间戳SUNDAY:17:00(并希望下一个星期日)将导致2016-07-10 19:00而不是2016-07-03 19:00。

#2


1  

You could use the DateTime Constructor to create a date and convert it like a normal DateTime, no?

您可以使用DateTime构造函数创建日期并将其转换为普通的DateTime,不是吗?

Check this example, and replace with what you need.

检查此示例,并替换为您需要的内容。

var theDate = new DateTime (DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, hours, minute, second);

From the doc:

从文档:

public DateTime(
    int year,
    int month,
    int day
)

DateTime date1 = new DateTime(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
//      8/18/2010 12:00:00 AM