There are some situations where it is useful to convert a
Date into a formatted character string within a application.
Note:- Confusion between “m” and “M”, small case “m” represent minutes while “M” represent Month. This is most common cause of error while converting String to date and back date to string. In short ddMMyy is not equal to ddmmyy .
Note:- Confusion between “m” and “M”, small case “m” represent minutes while “M” represent Month. This is most common cause of error while converting String to date and back date to string. In short ddMMyy is not equal to ddmmyy .
using System;
class DateFormat
{
static void Main()
{
String dateFormat1=DateTime.Now.ToString("MM/dd/yyyy");
Console.WriteLine(dateFormat1);
String dateFormat2=DateTime.Now.ToString("dd/MM/yyyy");
Console.WriteLine(dateFormat2);
String dateFormat3=DateTime.Now.ToString("MMM d, yyyy");
Console.WriteLine(dateFormat3);
String dateFormat4=DateTime.Now.ToString("dddd, MMMM d");
Console.WriteLine(dateFormat4);
String dateFormat5=DateTime.Now.ToString("MM/dd/yy");
Console.WriteLine(dateFormat5);
String dateFormat6=DateTime.Now.ToString("dd MMM yyyy");
Console.WriteLine(dateFormat6);
String dateFormat7=DateTime.Now.ToString("dd.MM.yyyy");
Console.WriteLine(dateFormat7);
String dateFormat8=DateTime.Now.ToString("yyMMdd");
Console.WriteLine(dateFormat8);
String dateFormat9=DateTime.Now.ToString("dddd");
Console.WriteLine(dateFormat9); // get only current day
String dateFormat10=DateTime.Now.ToString("MMMM");
Console.WriteLine(dateFormat10); // get only current month
}
}
That’s it!!…..Happy Programming...
No comments:
Post a Comment