@gomi_ningen's Website

Xamarin.iOSアレコレメモ

NSDate と DateTime の相互変換

  • 実際にはもうちょっとタイムゾーンとか色々気を使う必要あるけどざっくりまとめ
  • https://forums.xamarin.com/discussion/27184/convert-nsdate-to-datetime

NSDate => DateTime

ReferenceDate ってなんなの...(https://developer.apple.com/documentation/foundation/nsdate)

public static class FoundationExtensions { public static DateTime ToDateTime(this NSDate date) { // ref: https://developer.apple.com/documentation/foundation/nsdate var referenceDate = new DateTime(2001, 1, 1); return referenceDate.AddSeconds(date.SecondsSinceReferenceDate); } }
public static NSDate ToNSDate(this DateTime dateTime) { var universalTime = dateTime.ToUniversalTime(); var date = NSDate.FromTimeIntervalSinceReferenceDate((universalTime - referenceDate).TotalSeconds); return date; }

Copyright © 53ningen.com