Date in MM-dd-yy format

get date in MM-dd-yy format
Most of the time in C# code we need to format datetime as per requirement. Here we are going to discuss how to format date in MM-dd-yy format.So when we give input date as “10-29-11” then date will get formatted like “10/29/11”.

Sample code to format date in MM-dd-yy format [C#] :


using System;
using System.Globalization;

public class DateFormatSample
{
 public static void Main()
 {
  string dateToFormat = "10-29-11";
  string strFormatPattern = "MM-dd-yy";
  DateTime formattedDate;
  DateTime.TryParseExact(dateToFormat, strFormatPattern, null, 
  DateTimeStyles.None, out formattedDate);
  Console.WriteLine("Date '{0}' Formatted to {1:d}.", dateToFormat, formattedDate);
 }
}
//    output:
//    Date '12-30-11' Formatted to 12/30/2011.

Here MM displays in format 01 to 12, dd represents Day of the month with range from 01 to 31 and yy is year from 00 to 99.

0 Comments :

Post a Comment