C# Parse method of DateTime

C# Parse method of DateTime

Datetime.Parse method is used to parse string into valid date and time format. Here we will see example of DateTime parse in C#.net and Vb.net.

c# Date Format

[C#] program DateTime Parse Method.

using System;
using System.Globalization;
namespace DateAndTime.Examples
{
class Program
{
static void Main(string[] args)
{
DateTime parseDateString = DateTime.Parse("01-01-2012");
Console.WriteLine("Parsed date string is {0}", parseDateString);
DateTime parseDateStringProvider = DateTime.Parse("31-12-2011",new CultureInfo("en-GB")
,DateTimeStyles.None); Console.WriteLine("Parsed date string with provider is {0}", parseDateStringProvider); parseDateStringProvider = DateTime.Parse("12-31-2011", new CultureInfo("sv-SE"),
DateTimeStyles.None); Console.WriteLine("Parsed date string with provider is {0}", parseDateStringProvider); Console.ReadLine(); } } }

VB.net DateTime Pasrse Example

VB.net-Program
using System;
using System.Globalization;
namespace DateAndTime.Examples
{
class Program
{
static void Main(string[] args)
{
DateTime parseDateString = DateTime.Parse("01-01-2012");
Console.WriteLine("Parsed date string is {0}", parseDateString);
DateTime parseDateStringProvider = DateTime.Parse("31-12-2011",
new CultureInfo("en-GB"),DateTimeStyles.None); Console.WriteLine("Parsed date string with provider is {0}", parseDateStringProvider); parseDateStringProvider = DateTime.Parse("12-31-2011",
new CultureInfo("sv-SE"), DateTimeStyles.None); Console.WriteLine("Parsed date string with provider is {0}", parseDateStringProvider); Console.ReadLine(); } } }
Output
Parsed date string is 1/1/2012 12:00:00 AM
Parsed date string with provider is 12/31/2011 12:00:00 AM
Parsed date string with provider is 12/31/2011 12:00:00 AM

DateTime examples

How to format Time in C#.net and VB.net program.
DateTime Time Format string {HH:mm:ss}.
C#.net datetime Culture.
C#.net DateTime Month.
string was not recognised as a valid datetime
Get Today
DateTime in English UK format
 Get Day Of year

Tags:

DateTimeTryParse using C# and VB.net exampleDateTimeTryParse with IFormatProvider culture information using c#C# DateTime DifferenceDateTime ParseExact Time 10:10:13 AM hh mm ss tt nullIFormatProvider DateTime C#.netC# DateTimeC# DateTime ToString()DateTime.Today Vs DateTime.NowDateTime.ParseExact ExampleC# Convert String To DateTimeDateTime in C# current Date and Time

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.