c# - Xamarin 'iOS Foundation NSDate to C# DateTime conversion' 反之亦然不考虑夏令时

标签 c# ios datetime xamarin nsdate

我需要将 Cocoa NSDate 转换为 C# DateTime,反之亦然。

我正在使用以下方法来实现这一点:

 public static DateTime NSDateToDateTime (Foundation.NSDate date)
 {
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime (
        new DateTime (2001, 1, 1, 0, 0, 0));
   return reference.AddSeconds (date.SecondsSinceReferenceDate);
 }

 public static Foundation.NSDate DateTimeToNSDate (DateTime date)
 {
    DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime (
        new DateTime (2001, 1, 1, 0, 0, 0));
    return Foundation.NSDate.FromTimeIntervalSinceReferenceDate (
        (date - reference).TotalSeconds);
 }

但事实证明,这种方式并没有考虑夏令时。

例如。 DateTime 对象返回时间为凌晨 6 点的 NSDate

最佳答案

我很确定这是一种过时的方法。您现在可以将 NSDate 直接转换为 DateTime。

 Foundation.NSDate nsDate = // someValue
 DateTime dateTime = (DateTime)nsDate;

关于c# - Xamarin 'iOS Foundation NSDate to C# DateTime conversion' 反之亦然不考虑夏令时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470136/

相关文章:

android - DateUtils.getRelativeTimeSpanString() 在 Android 5 上因 NullPointerException 而失败

python - 日期时间范围索引 : datetimes that may not be in the index?

c# - 为什么要插入倍数?

ios - AVSpeechSynthesizer 语音未改变

ios - 使用 UIView 在 Swift 中归档 CSS "box-shadow"类似结果

python - 将对象转换为日期时间

c# - 调试错误 "The Type ' xx' 在未引用的程序集中定义”

c# - 启动异常在下一个请求时重试

c# - 列标题中带有点的 WPF Datagrid 绑定(bind)错误

ios - 具有多个数据数组的 UITableView