In this article,we will learn about different Date conversion from String to Date.
Introduction
Sometimes project requirements demand capturing Date in string,and sometimes it's also required to convert from string to Date-Time.
For working with String to Date conversion,we have to convert string date to Date then apply some format to have a Date-time values.
There are lots of Date Format as Microsoft has provided us,we can give our own format.
DateTime.Parse
supports many formats like dd/MM/yyyy, MMM-dd-yyyy, dd-MM-yyyy and many more formats.It is versatile and it can provoke a FormatException.
With Date object
ToString,DateTime.Parse or DateTime.ParseExact method,we can pass above date formats to get the date we desired.
DateTime.Parse:- It is a static method that
we can call without using a
Date-Time instance.
It returns a new Date-Time instance,which is represented as a Struct.
If time is not provided, "12 AM" is used.
TryParse:-Whenever we have a string containing a date and time that may
be invalid in formatting or that never occurred,we can use the DateTime.TryParse
static method.It is in the System namespace.
It converts the string to a Date-Time instance without raising an exception on errors.
Objective
Different Date conversion from String to Date.
Using the code
We will understand with an example:-
In the code-behind,we will create one method named convert_string_to_date() and in the Page Load event,we will call that method as shown below:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
convert_string_to_date();
}
}
/// <summary>
/// converts following string date into date pattern
/// </summary>
private void convert_string_to_date()
{
try
{
string ds1 = "Sep 2013";
string ds2 = "Monday 2 September 2013 14:15:33";
string ds3 = "2,9,13";
string ds4 = "2/9/2013 14:15:33";
string ds5 = "2:15 PM";
// 1st September 2013 00:00:00
DateTime dt1 = DateTime.Parse(ds1);
// 2nd September 2013 14:15:33
DateTime dt2 = DateTime.Parse(ds2);
// 2nd September 2013 00:00:00
DateTime dt3 = DateTime.Parse(ds3);
// 2nd September 2013 14:15:33
DateTime dt4 = DateTime.Parse(ds4);
// Current Date 14:15:00
DateTime dt5 = DateTime.Parse(ds5);
// Display the converted DateTime objects.
Response.Write(string.Format("String: {0} DateTime: {1}", ds1, dt1));
Response.Write("<br/>");
Response.Write(string.Format("String: {0} DateTime: {1}", ds2, dt2));
Response.Write("<br/>");
Response.Write(string.Format("String: {0} DateTime: {1}", ds3, dt3));
Response.Write("<br/>");
Response.Write(string.Format("String: {0} DateTime: {1}", ds4, dt4));
Response.Write("<br/>");
Response.Write(string.Format("String: {0} DateTime: {1}", ds5, dt5));
Response.Write("<br/>");
Response.Write("<br/>");
// Parse only strings containing LongTimePattern.
DateTime dt6 = DateTime.ParseExact("2:13:30 PM", "h:mm:ss tt", null);
// Parse only strings containing RFC1123Pattern.
DateTime dt7 = DateTime.ParseExact(
"Mon, 02 Sep 2013 14:13:30 GMT", "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'",
null);
// Parse only strings containing MonthDayPattern.
DateTime dt8 = DateTime.ParseExact("December 05", "MMMM dd", null);
DateTime dt9 = DateTime.ParseExact("December 05", "MMMM dd", System.Globalization.CultureInfo.InvariantCulture);
var dt10 = DateTime.ParseExact("24/12/2013", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Response.Write("<b>Below Output Displays the Converted DateTime objects</b>");
Response.Write("<br/>");
// Display the converted DateTime objects.
Response.Write("Date Conversion: " + dt6);
Response.Write("<br/>");
Response.Write("Date Conversion: " + dt7);
Response.Write("<br/>");
Response.Write("Date Conversion: " + dt8);
Response.Write("<br/>");
Response.Write("Date Conversion: " + dt9);
Response.Write("<br/>");
Response.Write("Date Conversion: " + dt10);
}
catch (Exception ex)
{
throw ex;
}
}
Output:
Conclusion
Today,we learned Different Date conversion from String to Date.
Reference
http://www.dotnetperls.com/datetime-parse