{"id":6083,"date":"2023-06-05T10:38:00","date_gmt":"2023-06-05T02:38:00","guid":{"rendered":"http:\/\/www.aiersheng.cc\/?p=6083"},"modified":"2023-06-05T10:38:00","modified_gmt":"2023-06-05T02:38:00","slug":"c%e4%b8%ad%e6%af%94%e8%be%83%e4%b8%a4%e4%b8%aa%e6%97%a5%e6%9c%9f%e5%a4%a7%e5%b0%8f%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.aiersheng.cc\/?p=6083","title":{"rendered":"C#\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u65e5\u671f\u5927\u5c0f\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>\/\/\/ <\/p>\n<summary>\n        \/\/\/ \u6bd4\u8f83\u4e24\u4e2a\u65e5\u671f\u5927\u5c0f<br \/>\n        \/\/\/ <\/summary>\n<p>        \/\/\/ <param name=\"dateStr1\">\u65e5\u671f1<\/param>\n        \/\/\/ <param name=\"dateStr2\">\u65e5\u671f2<\/param>\n        \/\/\/ <param name=\"msg\">\u8fd4\u56de\u4fe1\u606f<\/param>\n        public void CompanyDate(string dateStr1, string dateStr2,ref string msg) {<br \/>\n            \/\/\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61<br \/>\n            DateTime t1 = Convert.ToDateTime(dateStr1);<br \/>\n            DateTime t2 = Convert.ToDateTime(dateStr2);<br \/>\n            \/\/\u901a\u8fc7DateTIme.Compare()\u8fdb\u884c\u6bd4\u8f83\uff08\uff09<br \/>\n            int compNum = DateTime.Compare(t1, t2);<\/p>\n<p>            \/\/t1> t2<br \/>\n            if (compNum > 0) {<br \/>\n                msg = &#8220;t1:(&#8221; + dateStr1 + &#8220;)\u5927\u4e8e&#8221; + &#8220;t2(&#8221; + dateStr2 + &#8220;)&#8221;;<br \/>\n            }<br \/>\n            \/\/t1= t2<br \/>\n            if (compNum == 0) {<br \/>\n                msg = &#8220;t1:(&#8221; + dateStr1 + &#8220;)\u7b49\u4e8e&#8221; + &#8220;t2(&#8221; + dateStr2 + &#8220;)&#8221;;<br \/>\n            }<br \/>\n            \/\/t1< t2\n            if (compNum < 0) {\n                msg = \"t1:(\" + dateStr1 + \")\u5c0f\u4e8e\" + \"t2(\" + dateStr2 + \")\";\n            }\n        }\n\u3000\u6211\u4eec\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65e5\u671f\u683c\u5f0f\u5e38\u7528DateTime.Pares() \u4f46\u662f\u8fd9\u4e2a\u5f62\u5f0f\u7684\u8f6c\u6362\u662f\u76f8\u5f53\u6709\u9650\u7684\uff0c\u6709\u4e9bC#\u662f\u4f1a\u4e0d\u61c2\u4f60\u5199\u5165\u7684\u65e5\u671f\u683c\u5f0f\u7684\u598220031231\u3002\u90a3\u4e48\u7c7b\u4f3c \"20100101\" \u6216\u8005\u5176\u5b83\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\u5982\u4f55\u8f6c\u6362\u6210\u65e5\u671f\u578b\u5462?\n\n\n\u4e00\u3001\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f \n\nDateTime dt=Convert.ToDateTime(\"20100101\".Substring(0,4)+\"-\"+\"20100101\".Substring(4,2)+\"-\"+\"20071107\".Substring(6,2));  \n\u4e8c\u3001Convert.ToDateTime(string)\n\nstring\u683c\u5f0f\u6709\u8981\u6c42\uff0c\u5fc5\u987b\u662fyyyy-MM-dd hh:mm:ss \n\n\u4e09\u3001Convert.ToDateTime(string, IFormatProvider)\n\nDateTime dt;\nDateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();\ndtFormat.ShortDatePattern = \"yyyy\/MM\/dd\";\ndt = Convert.ToDateTime(\"2011\/05\/26\", dtFormat);\n\n\u56db\u3001DateTime.ParseExact()\n\nstring dateString = \"20110526\";\nDateTime dt = DateTime.ParseExact(dateString, \"yyyyMMdd\", System.Globalization.CultureInfo.CurrentCulture);\n\/\/\u6216\u8005\nDateTime dt = DateTime.ParseExact(dateString, \"yyyyMMdd\", System.Globalization.CultureInfo.InvariantCulture);\n\n\u4e94\u3001DateTime.TryParse(string,out datetime)\n\n1\u3001\u66f4\u591a\u65f6\u5019\uff0c\u4f1a\u91c7\u7528DateTime.TryParse(string,out datetime)\u65b9\u6cd5\uff0c\u56e0\u4e3a\u6b64\u65b9\u6cd5\u6709\u5b89\u5168\u673a\u5236\uff0c\u5f53string\u5185\u5bb9\u4e0d\u6b63\u786e\u65f6\uff0c\u53ef\u4ee5\u8fd4\u56de\u65e5\u671f\u7684\u6700\u5c0f\u503cMinValue\u3002\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u7684bool\u503c\u5224\u65ad\u8f6c\u5316\u662f\u5426\u6210\u529f\u3002\u800cDateTime.ParseExact()\u9700\u8981\u6309\u7279\u5b9a\u7684\u683c\u5f0f\u6765\u8f6c\u6362\uff0c\u5bf9\u683c\u5f0f\u7684\u8981\u6c42\u6bd4\u8f83\u4e25\uff0c\u5982\u679cstring\u4e2d\u4e0d\u662f\u65e5\u671f\u5185\u5bb9\uff0c\u800c\u91cf\u7c7b\u4f3c\u201casdfasd\u201d\u7684\u5b57\u7b26\u4e32\uff0c\u5219\u4f1a\u51fa\u9519\u3002\n2\u3001\u7528DateTime.TryParse(string,out datetime)\u8f6c\u6362\u540e\uff0c\u5f97\u5230\u7684datetime\u53ef\u4ee5\u7528 datetime.ToString(\"ddd, MMM. dd\")\u6765\u8f6c\u6362\u4e3a\u7279\u6b8a\u9700\u6c42\u7684\u683c\u5f0f\uff0c\u6bd4\u8f83\u7075\u6d3b\u65b9\u4fbf\u3002\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/\/ \/\/\/ \u6bd4\u8f83\u4e24\u4e2a\u65e5\u671f\u5927\u5c0f \/\/\/&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-6083","post","type-post","status-publish","format-standard","hentry","category-zzjy"],"_links":{"self":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/posts\/6083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6083"}],"version-history":[{"count":0,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=\/wp\/v2\/posts\/6083\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiersheng.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}