Posts

Showing posts with the label DateTime

C# DateTime Formatting

C# Offers a wide variety of formatting date times using the ToString method. Below you will see the various options you can use and the results. Format Result DateTime.Now.ToString("MM/dd/yyyy") 05/29/2015 DateTime.Now.ToString("dddd, dd MMMM yyyy") Friday, 29 May 2015 DateTime.Now.ToString("dddd, dd MMMM yyyy") Friday, 29 May 2015 05:50 DateTime.Now.ToString("dddd, dd MMMM yyyy") Friday, 29 May 2015 05:50 AM DateTime.Now.ToString("dddd, dd MMMM yyyy") Friday, 29 May 2015 5:50 DateTime.Now.ToString("dddd, dd MMMM yyyy") Friday, 29 May 2015 5:50 AM DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss") Friday, 29 May 2015 05:50:06 DateTime.Now.ToString("MM/dd/yyyy HH:mm") 05/29/2015 05:50 DateTime.Now.ToString("MM/dd/yyyy hh:mm tt") 05/29/2015 05:50 AM DateTime.Now.ToString("MM/dd/yyyy H:mm") 05/29/2015 5:50 DateTime.Now.ToString("MM/dd/yyyy h:mm tt") 05/29/2015 5:5...

C# get the number of days in a month

Image
Ok this isn't the longest blog post, but it contains a handy little code snippet. Sometimes you find yourself needing to know the number of days in any given month. The code below will get you the last day of the month. I'm using the current month but you could use any month. You just need to know the month and year you're looking for. The magic lies in the DateTime.DaysInMonth method. You pass in the year and month and it returns the number of days in that month. The code below is pretty self explanatory. static void Main( string[] args ) { DateTime now = DateTime.Now; int daysInMonth = DateTime.DaysInMonth( now.Year, now.Month ); String dateFormat = String.Format( "Last day of the month is - {0}/{1}/{2}", now.Month, daysInMonth, now.Year); Console.WriteLine( dateFormat); Console.ReadKey(); } And a brief picture showing the output, obviously I ran this is October 2011.

C# format date times for different locals (localization and globalization), DateTimeFormatInfo

Image
Working on a project recently I found we were presenting date/time data to a variety of users in various countries. Each country uses it's own date/time formatting. Here in the US it's often MM/dd/yyyy, however in Europe it's often dd/MM/yyyy and in Asia they don't even have the slashes they use special characters that designate year, month, and day. At first we were going to come up with localized versions of all the possible date/time combinations you can display (June 6th, 1977, 6/25/77, etc) but Microsoft has an even better solution already in place. static void Main( string[] args ) { DateTime dt = DateTime.Now; // let's create some different date times for different cultures // JAPANESE CultureInfo culture = new CultureInfo("ja-JP"); DateTimeFormatInfo formatInfo = culture.DateTimeFormat; String time = dt.ToString( formatInfo.FullDateTimePattern, culture ); MessageBox.Show( time );...

C# Formatting DateTime to a String

Often times you find yourself needing to convert a DateTime object into a useful string representation.  Here are some basic parameters to the DateTime.ToString() method. MM/dd/yyyy 08/25/2010 dddd, dd MMMM yyyy Tuesday, 25 August 2010 dddd, dd MMMM yyyy HH:mm Tuesday, 25 August 2010 06:30 dddd, dd MMMM yyyy hh:mm tt Tuesday, 25 August 2010 06:30 AM dddd, dd MMMM yyyy H:mm Tuesday, 25 August 2010 6:30 dddd, dd MMMM yyyy h:mm tt Tuesday, 25 August 2010 6:30 AM dddd, dd MMMM yyyy HH:mm:ss Tuesday, 25 August 2010 06:30:07 MM/dd/yyyy HH:mm 08/25/2010 06:30 MM/dd/yyyy hh:mm tt 08/25/2010 06:30 AM MM/dd/yyyy H:mm 08/25/2010 6:30 MM/dd/yyyy h:mm tt 08/25/2010 6:30 AM MM/dd/yyyy h:mm tt 08/25/2010 6:30 AM MM/dd/yyyy h:mm tt 08/25/2010 6:30 AM MM/dd/yyyy HH:mm:ss 08/25/2010 06:30:07 MMMM dd August 25 MMMM dd August 25 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2010-08-25T06:30:07.7199252-04:...

C# get current fiscal year stop and end dates

The following two functions can be used to determine the start and stop dates of the current fiscal year. They assume a standard FY of 10/1 - 9/30. The same logic applies if you want to modify them with different start dates. You will just need to use whatever DateTime values you want instead of the current. Get the start date // get the current month and year int month = DateTime.Now.Month; int year = DateTime.Now.Year; // if month is october or later, the FY started 10-1 of this year // else it started 10-1 of last year return month > 9 ? new DateTime( year, 10, 1 ) : new DateTime( year - 1, 10, 1 ); Get the stop date // get the current month and year int month = DateTime.Now.Month; int year = DateTime.Now.Year; // if month is october or later, the FY ends 9/30 next year // else it ends 9-30 of this year return month > 9 ? new DateTime( year + 1, 9, 30 ) : new DateTime( year, 9, 30 );