{"id":449,"date":"2017-08-18T09:57:24","date_gmt":"2017-08-18T00:57:24","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=449"},"modified":"2023-09-08T10:53:17","modified_gmt":"2023-09-08T01:53:17","slug":"c-timezone-%ec%93%b8%eb%aa%a8-%ec%97%86%ec%9d%84%ea%b2%83-%ea%b0%99%ec%9d%8c","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=449","title":{"rendered":"C#  ToUniversalTime \uc73c\ub85c \uc0ac\uc6a9\ud558\uae30"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\ucd94\ucc9c  : ToUniversalTime<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \" title=\"sample 2\"> static void Main(string[] args)\n        {\n            string DepartureTime = \"2023-09-09T11:40:00.000+08:00\";\n\n            string sDiffOfDatesTime = string.Empty;\n\n            try\n            {\n                DateTime Today = DateTime.Now.ToUniversalTime();\n\n                DateTime DepDate = DateTime.Parse(DepartureTime).ToUniversalTime();\n\n                var diffOfDates = DepDate.Subtract(Today);\n\n                if (diffOfDates.Days &gt; 0)\n                {\n                    sDiffOfDatesTime = diffOfDates.Days + \"D\";\n                }\n                else\n                {\n                    sDiffOfDatesTime = diffOfDates.Hours + \"H\";\n                }\n\n            }\n            catch\n            {\n                sDiffOfDatesTime = \"\";\n            }\n\n            Console.WriteLine(sDiffOfDatesTime);\n\n            Console.ReadLine();\n\n        }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \" title=\"sample1\">static void Main(string[] args)\n        {   \n            \/\/1. \uadf8 \uc2dc\uac04\uc758 \ud0c0\uc784\uc874 ID \ub97c  \uad6c\ud55c\ub2e4. \n            \/\/2. \uc2dc\uac04\uc744 \ud0c0\uc784\uc874 \uc2dc\uac04\uc73c\ub85c \ubcc0\uacbd\n            \/\/3. \ucc28\uc774 \ub9cc\ud07c\uc758 \uc2dc\uac04\uc744 \uad6c\ud55c\ub2e4.\n\n            string DepartureTime = \"2023-09-09T11:40:00.000+08:00\";\n\n            string reg = @\".000([-+]{1}[0-9]{2}:[0-9]{2})\";\n            var rx = new Regex(reg, RegexOptions.None);\n            var matches = rx.Matches(DepartureTime);\n\n            if (matches.Count &gt; 0)\n            {\n                string offset = matches[0].Groups[1].ToString();\n\n                TimeSpan ts = TimeSpan.Parse(offset.Replace('+',' ').Trim());\n                var sBaseUtcOffset = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x =&gt; x.BaseUtcOffset == ts);  \/\/+07:00 \ub85c \uc2dc\uac04\ub300 id \ucd94\ucd9c\n\n                DateTime cstTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, sBaseUtcOffset.Id.ToString()); \/\/\ud604\uc7ac \uc2dc\uac04\uc744 =&gt;  \uc2dc\uac04\ub300\ub85c \uc218\uc815 \n\n                Console.WriteLine(cstTime);\n            }\n\n            Console.ReadLine();\n\n        }<\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">using System.Linq;\nusing System.Text;\nusing System.Collections.ObjectModel;\n\nnamespace ConsoleTime\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n\n            DateTime thisDate = new DateTime(2007, 3, 10, 0, 0, 0);\n            DateTime dstDate = new DateTime(2007, 6, 10, 0, 0, 0);\n            DateTimeOffset thisTime;\n\n            thisTime = new DateTimeOffset(dstDate, new TimeSpan(-7, 0, 0));\n            ShowPossibleTimeZones(thisTime);\n\n            thisTime = new DateTimeOffset(thisDate, new TimeSpan(-6, 0, 0));\n            \n            ShowPossibleTimeZones(thisTime);\n\n            thisTime = new DateTimeOffset(thisDate, new TimeSpan(+1, 0, 0));\n            ShowPossibleTimeZones(thisTime);\n\n            Console.ReadLine();\n        }\n\n        private static void ShowPossibleTimeZones(DateTimeOffset offsetTime)\n        {\n            TimeSpan offset = offsetTime.Offset;\n            ReadOnlyCollection&lt;TimeZoneInfo&gt; timeZones;\n\n            Console.WriteLine(\"{0} could belong to the following time zones:\",\n                              offsetTime.ToString());\n            \/\/ Get all time zones defined on local system\n            timeZones = TimeZoneInfo.GetSystemTimeZones();\n            \/\/ Iterate time zones \n            foreach (TimeZoneInfo timeZone in timeZones)\n            {\n                \/\/ Compare offset with offset for that date in that time zone\n                if (timeZone.GetUtcOffset(offsetTime.DateTime).Equals(offset))\n                    Console.WriteLine(\"   {0}\", timeZone.DisplayName);\n            }\n            Console.WriteLine();\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\uacb0\uacfc<br><a href=\"http:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2017\/08\/Timezone.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-450\" src=\"http:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2017\/08\/Timezone-300x155.jpg\" alt=\"\" width=\"300\" height=\"155\" srcset=\"https:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2017\/08\/Timezone-300x155.jpg 300w, https:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2017\/08\/Timezone.jpg 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n\n\n\n<p> \ucc38\uc870 : https:\/\/stackoverflow.com\/questions\/1201378\/how-does-datetime-touniversaltime-work<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ucd94\ucc9c : ToUniversalTime 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,\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=449\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-449","post","type-post","status-publish","format-standard","hentry","category-c-form"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=449"}],"version-history":[{"count":7,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":873,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/449\/revisions\/873"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}