boutique replica bags up ideas

the best replique rolex and prices here.

julia highlight 99j hair color 10a quality straight human hair lace front wigs 8 - 24 inches pre plucked hairline 13x4 inches lace front brazilian wig onlinefor sale

C# – Convert a String to a DateTime Example

Updated on     Kisan Patel

In this tutorial, we will teach you how to convert String to a DateTime using Parse and ParseExact method of the DateTime class in C#.

Using Parse Method

The DateTime.Parse method can be used to generate a DateTime object from a given string. If you do not provide date, the current date is used. If you do not provide time, the time “12 AM” is used. if any error while executing Parse method, it throws a System.FormatException exception.

using System;

namespace ConsoleApp_Parse
{
    class Program
    {
        static void Main(string[] args)
        {
            string date = "2014-06-25";
            DateTime time = DateTime.Parse(date);
            Console.WriteLine(time);
        }
    }
}

the output of the above C# program…

datetime_parse_method

Using ParseExact Method

If you want to ensure that DateTime parses only strings that match specific string format then use the DateTime.ParseExact method. The DateTime.ParseExact method takes three parameters as shown in below example.

using System;

namespace ConsoleApp_Parse
{
    class Program
    {
        static void Main(string[] args)
        {
            string dateString = "Wed 25 Jun 9:30 AM 2014";
            string format = "ddd dd MMM h:mm tt yyyy";

            DateTime dateTime = DateTime.ParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture);
            Console.WriteLine(dateTime);
        }
    }
}

the output of the above C# program…

datetime_parseexact_method

DateTime.Parse and DateTime.ParseExact Method Example

using System;

namespace ConsoleApp_Parse
{
    class Program
    {
        static void Main(string[] args)
        {
            string parse1 = "Jun 2014";
            string parse2 = "Wednesday 25 June 2014 14:15:33";
            string parse3 = "5,9,5";
            string parse4 = "6/25/2014 14:15:33";
            string parse5 = "2:15 PM";

            // Display the converted DateTime objects using Parse.
            Console.WriteLine("String: {0} DateTime: {1}", parse1, DateTime.Parse(parse1));
            Console.WriteLine("String: {0} DateTime: {1}", parse2, DateTime.Parse(parse2));
            Console.WriteLine("String: {0} DateTime: {1}", parse3, DateTime.Parse(parse3));
            Console.WriteLine("String: {0} DateTime: {1}", parse4, DateTime.Parse(parse4));
            Console.WriteLine("String: {0} DateTime: {1}", parse5, DateTime.Parse(parse5));

            // Display the converted DateTime objects using ParseExact.
            Console.WriteLine(DateTime.ParseExact("9:30:30 AM", "h:mm:ss tt", null));
            Console.WriteLine(DateTime.ParseExact("25-06-2014", "dd-MM-yyyy", null));
        }
    }
}

the output of the above C# program…

convert_string_to_datetime_c_sharp


C#

Leave a Reply