추천 : ToUniversalTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
static void Main(string[] args) { string DepartureTime = "2023-09-09T11:40:00.000+08:00"; string sDiffOfDatesTime = string.Empty; try { DateTime Today = DateTime.Now.ToUniversalTime(); DateTime DepDate = DateTime.Parse(DepartureTime).ToUniversalTime(); var diffOfDates = DepDate.Subtract(Today); if (diffOfDates.Days > 0) { sDiffOfDatesTime = diffOfDates.Days + "D"; } else { sDiffOfDatesTime = diffOfDates.Hours + "H"; } } catch { sDiffOfDatesTime = ""; } Console.WriteLine(sDiffOfDatesTime); Console.ReadLine(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
static void Main(string[] args) { //1. 그 시간의 타임존 ID 를 구한다. //2. 시간을 타임존 시간으로 변경 //3. 차이 만큼의 시간을 구한다. string DepartureTime = "2023-09-09T11:40:00.000+08:00"; string reg = @".000([-+]{1}[0-9]{2}:[0-9]{2})"; var rx = new Regex(reg, RegexOptions.None); var matches = rx.Matches(DepartureTime); if (matches.Count > 0) { string offset = matches[0].Groups[1].ToString(); TimeSpan ts = TimeSpan.Parse(offset.Replace('+',' ').Trim()); var sBaseUtcOffset = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.BaseUtcOffset == ts); //+07:00 로 시간대 id 추출 DateTime cstTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, sBaseUtcOffset.Id.ToString()); //현재 시간을 => 시간대로 수정 Console.WriteLine(cstTime); } Console.ReadLine(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System.Linq; using System.Text; using System.Collections.ObjectModel; namespace ConsoleTime { class Program { static void Main(string[] args) { DateTime thisDate = new DateTime(2007, 3, 10, 0, 0, 0); DateTime dstDate = new DateTime(2007, 6, 10, 0, 0, 0); DateTimeOffset thisTime; thisTime = new DateTimeOffset(dstDate, new TimeSpan(-7, 0, 0)); ShowPossibleTimeZones(thisTime); thisTime = new DateTimeOffset(thisDate, new TimeSpan(-6, 0, 0)); ShowPossibleTimeZones(thisTime); thisTime = new DateTimeOffset(thisDate, new TimeSpan(+1, 0, 0)); ShowPossibleTimeZones(thisTime); Console.ReadLine(); } private static void ShowPossibleTimeZones(DateTimeOffset offsetTime) { TimeSpan offset = offsetTime.Offset; ReadOnlyCollection<TimeZoneInfo> timeZones; Console.WriteLine("{0} could belong to the following time zones:", offsetTime.ToString()); // Get all time zones defined on local system timeZones = TimeZoneInfo.GetSystemTimeZones(); // Iterate time zones foreach (TimeZoneInfo timeZone in timeZones) { // Compare offset with offset for that date in that time zone if (timeZone.GetUtcOffset(offsetTime.DateTime).Equals(offset)) Console.WriteLine(" {0}", timeZone.DisplayName); } Console.WriteLine(); } } } |
참조 : https://stackoverflow.com/questions/1201378/how-does-datetime-touniversaltime-work