Click here to Skip to main content
15,117,925 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How Extract date from following given string????

"2015-06-01t00:00:00"

Thanks in Adavance
Posted
Comments
Liju Sankar 28-Jul-15 0:12am
   
DateTime.Parse("StringDATE").ToString("yyyy-MM-dd") is a shorthand way of doing it. you can modify the date format as you want.

One way is to use DateTime.TryParse and then the Date property. Consider the following
C#
System.DateTime a;
if(System.DateTime.TryParse("2015-06-01t00:00:00", out a))
{
   System.Diagnostics.Debug.Print(a.Date.ToString());
}

However, DateTime always contains the time portion so it's up to the code to use it or not.

In the real situation, just remember to check if the TryParse worked by investigating the boolean return value. See DateTime.TryParse[^]
   
v5
Comments
Naveen Kumar Tiwari 27-Jul-15 4:44am
   
thank u so much
Wendelius 27-Jul-15 4:46am
   
You're welcome :)
I prefer using DateTime.TryParseExact to help ensure the month and day are never transposed due to an unexpected difference between regional settings in my local development environment and regional settings in the environment where the code might eventually run (e.g. a remote web server).

If the input string always follows the same pattern (e.g. "yyyy-MM-DDtHH:mm:ss") then my preferred solution looks like this:

C#
var culture = new CultureInfo("en-US");
var input = "2015-06-01t00:00:00";
DateTime parsed;
if ( DateTime.TryParseExact(input, @"yyyy-MM-dd\tHH:mm:ss", culture, DateTimeStyles.None, out parsed ) )
{
  // parsed.Date contains the date part of the input string
}
else
{
  // Handle the unexpected format
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900