How do I get today's date in C# in mm/dd/yyyy format? - Stack ...

文章推薦指數: 80 %
投票人數:10人

string today = DateTime.Today.ToString("M/d");. Home Public Questions Tags Users Collectives ExploreCollectives FindaJob Jobs Companies Teams StackOverflowforTeams –Collaborateandshareknowledgewithaprivategroup. CreateafreeTeam WhatisTeams? Teams CreatefreeTeam CollectivesonStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore HowdoIgettoday'sdateinC#inmm/dd/yyyyformat? AskQuestion Asked 13years,6monthsago Modified 3years,2monthsago Viewed 378ktimes 111 6 HowdoIgettoday'sdateinC#inmm/dd/yyyyformat? Ineedtosetastringvariabletotoday'sdate(preferablywithouttheyear),butthere'sgottobeabetterwaythanbuildingitmonth-/-dayonepieceatatime. BTW:I'mintheUSsoM/ddwouldbecorrect,e.g.September11this9/11. Note:ananswerfromkronozcameinthatdiscussedinternationalization,andIthoughtitwasawesomeenoughtomentionsinceIcan'tmakeitan'accepted'answeraswell. kronoz'sanswer c#date Share Follow editedMay23,2017at12:10 CommunityBot 111silverbadge askedAug28,2008at16:37 KengKeng 49.8k3232goldbadges7979silverbadges109109bronzebadges 1 4 M/ddwouldshowJan1as1/01.Doyouwant1/01orjust1/1? – JoelCoehoorn Sep26,2008at16:35 Addacomment  |  8Answers 8 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 220 DateTime.Now.ToString("M/d/yyyy"); http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx Share Follow editedMay22,2013at16:13 Bakudan 18.4k99goldbadges5050silverbadges7171bronzebadges answeredAug28,2008at16:37 JoelCoehoornJoelCoehoorn 376k109109goldbadges542542silverbadges777777bronzebadges 1 2 hh:mm:ssgivesthetime.DateTime.Now.ToString("M/d/yyyyhh:mm:ss"); – JoeCotton Nov9,2016at19:14 Addacomment  |  23 Nottobehorriblypedantic,butifyouareinternationalisingthecodeitmightbemoreusefultohavethefacilitytogettheshortdateforagivenculture,e.g.:- usingSystem.Globalization; usingSystem.Threading; ... varcurrentCulture=Thread.CurrentThread.CurrentCulture; try{ Thread.CurrentThread.CurrentCulture=CultureInfo.CreateSpecificCulture("en-us"); stringshortDateString=DateTime.Now.ToShortDateString(); //DosomethingwithshortDateString... }finally{ Thread.CurrentThread.CurrentCulture=currentCulture; } Thoughclearlythe"m/dd/yyyy"approachisconsiderablyneater!! Share Follow answeredAug28,2008at17:05 ljsljs 35.8k3535goldbadges104104silverbadges124124bronzebadges Addacomment  |  15 DateTime.Now.ToString("dd/MM/yyyy"); Share Follow editedOct31,2011at5:49 ChrisFrederick 5,34233goldbadges3434silverbadges4242bronzebadges answeredAug28,2008at16:38 CorinBlaikieCorinBlaikie 17.1k99goldbadges3535silverbadges3939bronzebadges Addacomment  |  9 Ifyouwantitwithouttheyear: DateTime.Now.ToString("MM/DD"); DateTime.ToString()hasalotofcoolformatstrings: http://msdn.microsoft.com/en-us/library/aa326721.aspx Share Follow answeredAug28,2008at16:41 FlySwatFlySwat 165k7171goldbadges243243silverbadges309309bronzebadges Addacomment  |  8 stringtoday=DateTime.Today.ToString("M/d"); Share Follow answeredAug28,2008at16:42 BillyJoBillyJo 1,2871919silverbadges3232bronzebadges Addacomment  |  8 DateTime.Now.Date.ToShortDateString() isculturespecific. Itisbesttostickwith: DateTime.Now.ToString("d/MM/yyyy"); Share Follow editedSep24,2012at19:14 PeterMortensen 29.8k2121goldbadges9898silverbadges124124bronzebadges answeredAug28,2008at16:41 CorinBlaikieCorinBlaikie 17.1k99goldbadges3535silverbadges3939bronzebadges Addacomment  |  5 DateTime.Now.Date.ToShortDateString() Ithinkthisiswhatyouarelookingfor Share Follow editedApr10,2016at4:38 Pang 8,994145145goldbadges8282silverbadges117117bronzebadges answeredAug28,2008at16:39 JoshMeinJoshMein 27.2k1212goldbadges7373silverbadges8484bronzebadges Addacomment  |  3 Orwithouttheyear: DateTime.Now.ToString("M/dd") Share Follow editedApr10,2016at4:38 Pang 8,994145145goldbadges8282silverbadges117117bronzebadges answeredAug28,2008at16:41 EBGreenEBGreen 35.1k1111goldbadges6161silverbadges8181bronzebadges Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedc#dateoraskyourownquestion. TheOverflowBlog AIandnanotechnologyareworkingtogethertosolvereal-worldproblems FeaturedonMeta WhatgoesintositesponsorshipsonSE? StackExchangeQ&AaccesswillnotberestrictedinRussia Shouldweburninatethe[term]tag? NewUserExperience:DeepDiveintoourResearchontheStagingGround–How... AskWizardforNewUsersFeatureTestisnowLive Visitchat Linked -3 HowdoIgetthesystemdateandtimeandstoreinavariable -8 Getcurrentdateinpersonalizedformatinc# Related 4154 Howtoenumerateanenum 2022 HowtoreturnonlytheDatefromaSQLServerDateTimedatatype 1474 WherecanIfinddocumentationonformattingadateinJavaScript? 1852 Detectingan"invaliddate"DateinstanceinJavaScript 1305 YYYY-MM-DDformatdateinshellscript 746 HowtogetthecurrenttimeinYYYY-MM-DDHH:MI:Sec.MillisecondformatinJava? 2763 HowdoIgetthecurrentdateinJavaScript? 3003 HowtoformataJavaScriptdate 1230 HowtogetcurrenttimeanddateinAndroid 809 FormatJavaScriptdateasyyyy-mm-dd HotNetworkQuestions Howtomotivatestafftoattendapaidcertification? WhatdoestheLorentzfactorrepresent? HowmanyholdstobeIRcurrent? Isthereanyexampleofadependentproductthatmakessenseinanon-type-theorycontext? IsareturnairticketabsolutelynecessaryforSchengenbusinesstrip? Howtopreformlocwithoneconditionthatincludetwocolumns HowToshowhiddenFolders/Files Iturnedabreakeroff,andnowthesecurityalarmsystemwon’tstopbeeping Howdoeslicensingsoftwarenotimplyownership?Don'tIownaWindowsoperatingsystemonceIpayforit? IsthereatimelimittopublishPhDresearch? Whatisthecollectivename/termgiventothepartofacurrencyvaluebeforeandafterthepoint? Lookingforaseriesaboutyounggirlwhowascreatedinalabbyhermum Sandboxoverflow Meaningof"orwehaveheadphonesorwedon't,so..." WhatcanweeatinthelateCretaceous? IfIdisagreewiththeresearchcommunity,shouldIreviewpapersaccordingtomybeliefsorthecommunity's? Reasonssomeonewouldavoidworkingoncareergrowth Howtodrainatoiletbowl? Minussignnotaligningwithfraction Isitalrighttochangepropertiesto@apisotheycanbetestedinunittestinlwcjest Willunevenspeedberoundedupordown? Checkifacommandexistsrevisited Foregoingthebackgroundincomicsbooks WhywasTurningRedsetinCanadain2002andnotinpresentdayAmerica? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-cs Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?