Your browser does not support our blog javascript

tostring

search for more blogs here

 
Home - Take this blog! - Get your Author's Pass Here - Submit Comments Below

Using and formatting DateTime and TimeSpan data types

Posted by ~Ray @ 2008-11-13 12:19:34


One of the most useful constructs in the. NET Framework is the ease of use for date/time manipulation. I need to manipulate dates and time frequently either displaying them or parsing strings containing them. DateTime dt;dt = DateTime. MinValue;Console. WriteLine(dt. ToString());dt = DateTime. MaxValue;Console. WriteLine(dt. ToString());dt = DateTime. Today;Console. WriteLine(dt. ToString());dt = DateTime. Now;Console. WriteLine(dt. ToString());dt = DateTime. UtcNow;Console. WriteLine(dt. ToString()); represents today with the time component in Universal Coordinated Time. Taking the date of this post again it’ll be year 2007. November 2nd. And (for example) the time is 1st hour. 1st minute and 34th second. I’m in Singapore so the time zone offset is 8 hours ahead of UTC. is particularly useful when you deal with international data where the dates and times can span across many countries. In this case having one standard time offset makes manipulating and displaying dates and times easy. Then people won’t keep asking “Is this American time or London time or Singapore time?” Just display and let the users add or subtract their own time zone offset to obtain data in their local time. DateTime dt;// 2nd Nov 2007. 1:15 pm. 45th second. 853rd milliseconddt = new DateTime(2007. 11. 2. 13. 15. 45. 853);Console. WriteLine(dt. ToString());// get the same date by parsing a stringdt = DateTime. ParseExact("20071102131545". "yyyyMMddHHmmss" null);Console. WriteLine(dt. ToString());// get the same date by parsing a string formatted differentlydt = DateTime. ParseExact("02/11/2007 13:15:45". "dd/MM/yyyy HH:mm:ss" null);Console. WriteLine(dt. ToString()); I like to have exact control over how a date/time string is interpreted and I want any culture-specific information to be left out of the parsing. If you know exactly what you have and what you need it’s better to do it yourself rather than letting the code do implicit conversions. I’m paranoid that way… DateTime dt;dt = new DateTime(2007. 11. 2. 13. 15. 45. 853);// get 2 Nov 2007 01:15 PMConsole. WriteLine(dt. ToString("d MMM yyyy hh:mm tt"));// get November 02. 2007 13:15Console. WriteLine(dt. ToString("MMMM dd yyyy HH:mm"));// get 2007-11-02T13:15:45.8+08:00Console. WriteLine(dt. ToString("yyyy-MM-ddTHH:mm:ss fzzz")); DateTime dtStart dtEnd;int iDaysInMonth = 0;dtStart = DateTime. Now;dtStart = new DateTime(dtStart. Year dtStart. Month. 1. 0. 0. 0. 0);dtEnd = DateTime. Now;iDaysInMonth = DateTime. DaysInMonth(dtEnd. Year dtEnd. Month);dtEnd = new DateTime(dtEnd. Year dtEnd. Month iDaysInMonth. 23. 59. 59. 999);Console. WriteLine("The first day of the month is {0}" dtStart. ToString("dd/MM/yyyy HH:mm:ss"));Console. WriteLine("The last day of the month is {0}" dtEnd. ToString("dd/MM/yyyy HH:mm:ss")); function. The number of days in the specific month is also the last day of the month. No fuss no muss. This is way better than you implementing a custom function which probably involves some arcane calculation with leap and non-leap years. If you do it wrongly you’ll end up with 28 days instead of 29 for certain February’s. The DateTime dtStart dtEnd;dtStart = new DateTime(2007. 10. 27);dtEnd = new DateTime(2007. 11. 2. 13. 15. 45. 853);TimeSpan ts = dtEnd - dtStart;Console. WriteLine(ts. Days);Console. WriteLine(ts. Hours);Console. WriteLine(ts. Milliseconds);Console. WriteLine(ts. Minutes);Console. WriteLine(ts. Seconds);Console. WriteLine(ts. Ticks);Console. WriteLine(ts. TotalDays);Console. WriteLine(ts. TotalHours);Console. WriteLine(ts. TotalMilliseconds);Console. WriteLine(ts. TotalMinutes);Console. WriteLine(ts. TotalSeconds); in the duration. Similarly for the other “Total”-prepended properties. Just print out the values to get a feel of the differences. Now for a more specific and practical example. How do we calculate the number of days between two dates? DateTime dtStart dtEnd;TimeSpan ts;dtStart = new DateTime(2007. 2. 26);dtEnd = new DateTime(2007. 3. 2);ts = dtEnd - dtStart;Console. WriteLine(ts. TotalDays);dtStart = new DateTime(2008. 2. 26);dtEnd = new DateTime(2008. 3. 2);ts = dtEnd - dtStart;Console. WriteLine(ts. TotalDays); The above gives 4 days (26 Feb. 27 Feb. 28 Feb and 1 Mar) and 5 days (as before plus 29 Feb) respectively. Note that the leap years are also taken care of. The end date is not included in the calculation. Since the end date is at the very start of the day (0th hour. 0th minute. 0th second) it’s not counted as far as duration is concerned. But what if you need the number of months instead? How do you do it? Well you don’t need DateTime dtStart dtEnd;dtStart = new DateTime(2006. 11. 15);dtEnd = new DateTime(2007. 2. 15);int iNumberOfMonths = (dtEnd. Year * 12 + dtEnd. Month) - (dtStart. Year * 12 + dtStart. Month);Console. WriteLine(iNumberOfMonths); The DateTime. MinValue represents year 0001. January 1st on the 0th hour the 0th minute and 0th second. The DateTime. MinValue represents year 9999. December 31st on the 23rd hour the 59th minute and 59th second. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> [ADVERTHERE]Related article:
http://polymathprogrammer.com/2007/11/02/using-and-formatting-datetime-and-timespan/


0 Comments:


No comments have been posted yet!

From:   Website:
Subject:   Code:
Message:


   

 


 

 

 





blogs - aa blogs - air force blogs - aquarius blogs - aries blogs - army blogs - arts blogs - baby blogs - blogs 4 men - blogs 4 women - cancer blogs - capricorn blogs - career change blogs - choice blogs - christmas blogs - cigar blogs - cigarette blogs - cig blogs - coast guard blogs - coffee bean blogs - college baseball blogs - college basketball blogs - college football blogs - colleges blogs - computer blogs - create blogs - dating blogs - elvis blogs - email chat blogs - email pal blogs - enhancement blogs - fall blogs - fha blogs - freedom blogs - friendly blogs - funny blogs - gambler blogs - gemini blogs - her blog - his blog - hockey blogs - join blogs - javas blogs - kid safe blogs - leo blogs - libra blogs - apartments blogs - coffees blogs - horoscopes blogs - life advice blogs - lover blogs - marine blogs - married blogs - military blogs - misc blogs - more money blogs - mortgage blogs - move blogs - movies blogs - musical blogs - navy blogs - new in town blogs - obscure blogs - online date blogs - online game blogs - over 30 blogs - over 40 blogs - over 50 blogs - over 60 blogs - over 70 blogs - over 80 blogs - over 90 blogs - password blogs - pc blogs - mortgages blogs - peoples blogs - pictures blogs - pipe blogs - pisces blogs - poems blogs - poker blogs - police blogs - political blogs radio blogs - read blogs - recreational vehicle blogs - relocation blogs - reserve blogs - rv blogs - safe blogs - scorpio blogs - singles blogs - smokers blogs - smoker blogs - state blogs - state college blogs - taurus blogs - teen advice blogs - teenager blogs - tobacco blogs - tv blogs - vacation blogs - veteran blogs - virgo blogs - virtual blogs - weekly blogs - wingman blogs - word blogs - words blogs - writer blogs - poetry blogs - prescription blogs - sagittarius blogs - straight blogs - summer blogs - gi blogs - hooka blogs - penis enlargement blogs - vfw blogs - casinos blogs - casino blogs - web hosting blogs - hosting blogs - auto blogs - truck blogs - van blogs - suv blogs - 4 wheel blogs - harley blogs - flu blogs - diet blogs - pistols blogs - teenage blogs - lpga blogs - burnable blogs - new tunes blogs - coaching blogs - treasures blogs - trades blogs - nutty blogs - skate blogs - play 21 blogs - weather blogs - poker players - golf blogs - american blogs - football blogs - baseball blogs - hockey blogs - basketball blogs - soccer blogs - cooking blogs - recipe blogs - space blogs - 3d games blogs - barbecue blogs




blogs home