DateTime Format In C#
文章推薦指數: 80 %
This blog describes how to format DateTime in C# with code sample. ... ToString("dddd, dd MMMM yyyy HH:mm:ss"), Friday, 29 May 2015 05:50:06. DateTimeFormatInC# SureshM UpdateddateSep19,2020 5.4m 0 41 SyntaxofC#DateTimeFormat.ThisblogdescribeshowtoformatDateTimeinC#withcodesample. facebook twitter linkedIn Reddit WhatsApp Email Bookmark expand C#DateTimeFormat DateandTimeinC#arehandledbyDateTimeclassinC#thatprovidespropertiesandmethodstoformatdatesindifferentdatetimeformats.ThisarticleblogexplainshowtoworkwithdateandtimeformatinC#. ThefollowingtabledescribesvariousC#DateTimeformatsandtheirresults.HereweseeallthepatternsoftheC#DateTime,format,andresults. Format Result DateTime.Now.ToString("MM/dd/yyyy") 05/29/2015 DateTime.Now.ToString("dddd,ddMMMMyyyy") Friday,29May2015 DateTime.Now.ToString("dddd,ddMMMMyyyy") Friday,29May201505:50 DateTime.Now.ToString("dddd,ddMMMMyyyy") Friday,29May201505:50AM DateTime.Now.ToString("dddd,ddMMMMyyyy") Friday,29May20155:50 DateTime.Now.ToString("dddd,ddMMMMyyyy") Friday,29May20155:50AM DateTime.Now.ToString("dddd,ddMMMMyyyyHH:mm:ss") Friday,29May201505:50:06 DateTime.Now.ToString("MM/dd/yyyyHH:mm") 05/29/201505:50 DateTime.Now.ToString("MM/dd/yyyyhh:mmtt") 05/29/201505:50AM DateTime.Now.ToString("MM/dd/yyyyH:mm") 05/29/20155:50 DateTime.Now.ToString("MM/dd/yyyyh:mmtt") 05/29/20155:50AM DateTime.Now.ToString("MM/dd/yyyyHH:mm:ss") 05/29/201505:50:06 DateTime.Now.ToString("MMMMdd") May29 DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK") 2015-05-16T05:50:06.7199222-04:00 DateTime.Now.ToString("ddd,ddMMMyyyHH’:’mm’:’ss‘GMT’") Fri,16May201505:50:06GMT DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss") 2015-05-16T05:50:06 DateTime.Now.ToString("HH:mm") 05:50 DateTime.Now.ToString("hh:mmtt") 05:50AM DateTime.Now.ToString("H:mm") 5:50 DateTime.Now.ToString("h:mmtt") 5:50AM DateTime.Now.ToString("HH:mm:ss") 05:50:06 DateTime.Now.ToString("yyyyMMMM") 2015May d->Representsthedayofthemonthasanumberfrom1through31. dd->Representsthedayofthemonthasanumberfrom01through31. ddd->Representstheabbreviatednameoftheday(Mon,Tues,Wed,etc). dddd->Representsthefullnameoftheday(Monday,Tuesday,etc). h->12-hourclockhour(e.g.4). hh->12-hourclock,withaleading0(e.g.06) H->24-hourclockhour(e.g.15) HH->24-hourclockhour,withaleading0(e.g.22) m->Minutes mm->Minuteswithaleadingzero M->Monthnumber(eg.3) MM->Monthnumberwithleadingzero(eg.04) MMM->AbbreviatedMonthName(e.g.Dec) MMMM->Fullmonthname(e.g.December) s->Seconds ss->Secondswithleadingzero t->AbbreviatedAM/PM(e.g.AorP) tt->AM/PM(e.g.AMorPM y->Year,noleadingzero(e.g.2015wouldbe15) yy->Year,leadingzero(e.g.2015wouldbe015) yyy->Year,(e.g.2015) yyyy->Year,(e.g.2015) K->Representsthetimezoneinformationofadateandtimevalue(e.g.+05:00) z->WithDateTimevaluesrepresentsthesignedoffsetofthelocaloperatingsystem'stimezonefrom CoordinatedUniversalTime(UTC),measuredinhours.(e.g.+6) zz->Asz,butwithleadingzero(e.g.+06) zzz->WithDateTimevaluesrepresentsthesignedoffsetofthelocaloperatingsystem'stimezonefromUTC,measuredinhoursandminutes.(e.g.+06:00) f->Representsthemostsignificantdigitoftheseconds'fraction;thatis,itrepresentsthetenthsofasecondinadateandtimevalue. ff->Representsthetwomostsignificantdigitsoftheseconds'fractionindateandtime fff->Representsthethreemostsignificantdigitsoftheseconds'fraction;thatis,itrepresentsthemillisecondsinadateandtimevalue. ffff->Representsthefourmostsignificantdigitsoftheseconds'fraction;thatis,itrepresentstheten-thousandthsofasecondinadateandtimevalue.Whileitispossibletodisplaytheten-thousandthsofasecondcomponentofatimevalue,thatvaluemaynotbemeaningful. fffff->Representsthefivemostsignificantdigitsoftheseconds'fraction;thatis,itrepresentsthehundred-thousandthsofasecondinadateandtimevalue. ffffff->Representsthesixmostsignificantdigitsoftheseconds'fraction;thatis,itrepresentsthemillionthsofasecondinadateandtimevalue. fffffff->Representsthesevenmostsignificantdigitsofthesecond'sfraction;thatis,itrepresentstheten-millionthsofasecondinadateandtimevalue. HereisacompleteC#codesamplethatusestheseformats. using System; namespace DateTimeFormatInCSharpSample { class Program { static void Main(string[] args) { // Get current DateTime.ItcanbeanyDateTimeobjectinyourcode. DateTime aDate = DateTime.Now; // Format Datetime in different formats and display them Console.WriteLine(aDate.ToString("MM/dd/yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy HH:mm:ss")); Console.WriteLine(aDate.ToString("MM/dd/yyyy HH:mm")); Console.WriteLine(aDate.ToString("MM/dd/yyyy hh:mm tt")); Console.WriteLine(aDate.ToString("MM/dd/yyyy H:mm")); Console.WriteLine(aDate.ToString("MM/dd/yyyy h:mm tt")); Console.WriteLine(aDate.ToString("MM/dd/yyyy HH:mm:ss")); Console.WriteLine(aDate.ToString("MMMM dd")); Console.WriteLine(aDate.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK")); Console.WriteLine(aDate.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’")); Console.WriteLine(aDate.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss")); Console.WriteLine(aDate.ToString("HH:mm")); Console.WriteLine(aDate.ToString("hh:mm tt")); Console.WriteLine(aDate.ToString("H:mm")); Console.WriteLine(aDate.ToString("h:mm tt")); Console.WriteLine(aDate.ToString("HH:mm:ss")); Console.WriteLine(aDate.ToString("yyyy MMMM")); Console.ReadKey(); } } } Abovecodesamplegeneratesthefollowingoutput. LearnMoreonDateTime HereisadetailedtutorialonDateTimeandformatting: WorkingwithDateTimeInC# LearnaboutDateTimeClassinC# CalculateDateDifferenceinC# DateandTimeFormatinC# NextRecommendedReading FilterDateTimeFromDataTableInC# LATESTBLOGS OptionsForLockingFieldOnBusinessProcessFlow Installng-bootstrapLibraryInAngular Ref,Out,andOptionalParametersinC# DifferenceBetweenRefandOutParameters SkillsRequiredForDifferentSectors UsageOfPartial,ParameterAndKeyofKeywords SetupLocalDatabaseInSQLServer DifferenceBetweenObservable,Eventemitter,andSubject It’sTimeToTidyUpYourCode:HowToUseVSCodeAutoformat BestPracticesForWritingSQLQueries ViewAll
延伸文章資訊
- 1DateTimeFormatInfo.GetAllDateTimePatterns 方法 - Microsoft ...
fffffffK 2014-08-28T12:28:30.0000000 // ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Thu, ... C# 複製. using...
- 2C# DateTime Format:Working with Date &Time format in C#|
Display date time in 24 hour format. DateTime.Now.ToString(“dd/MM/yyyy HH:mm:ss”);. Ans :07/03/20...
- 3DateTime Format In C#
This blog describes how to format DateTime in C# with code sample. ... ToString("dddd, dd MMMM yy...
- 4How to convert datetime in the format dd/MM/yyyy hh:mm:ss:tt ...
- 5[C#] DateTime toString格式化處理 - 克里斯小圭的碎碎念
最近常常在做時間格式轉換, 可是每次寫一寫都老是忘記,將時間轉化成yyyy/MM/dd hh:mm:ss的格式看起來似乎沒什麼錯,但hh這邊可是大有問題主要原因 ...