Search results
21 mar 2011 · DateTime.Parse() will try figure out the format of the given date, and it usually does a good job. If you can guarantee dates will always be in a given format then you can use ParseExact(): string s = "2011-03-21 13:26"; DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
Restore (round-trip) a date and time value created by a formatting operation. Pass the "o" or "r" standard format string to the ToString (String) method, and call the Parse (String, IFormatProvider, DateTimeStyles) overload with DateTimeStyles.RoundtripKind.
4 paź 2022 · The following example illustrates the use of the DateTime.Parse method to convert a string into a DateTime. This example uses the culture associated with the current thread. If the CultureInfo associated with the current culture can't parse the input string, a FormatException is thrown.
13 maj 2021 · You can convert a string to a DateTime (parse) with one of these methods: DateTime.Parse() (or TryParse). Use this to parse a variety of standard formats (culture-specific or ISO-8601) DateTime.ParseExact() (or TryParseExact). Use this to parse from a specific format. I’ll show examples of using both methods for parsing various DateTime formats.
22 lip 2024 · One of the simplest ways to convert a string to a DateTime object is by using the DateTime.Parse method. This method automatically detects the format of the input string and converts it to a DateTime object. string dateString = "2024-07-22"; DateTime dateTime = DateTime.Parse(dateString); Console.WriteLine(dateTime); Using DateTime.ParseExact.
6 paź 2023 · In this guide, we’ve covered the basics of parsing DateTime strings in C# using methods like DateTime.Parse and DateTime.ParseExact. We’ve also explored formatting DateTime objects into custom string representations using DateTime.ToString and custom format strings.
7 kwi 2023 · Dot Net Perls. DateTime.Parse. In C# DateTime.Parse () handles many formats. We get a DateTime from a string. When we know the format is correct, use Parse—otherwise, prefer TryParse. We can parse formatted date and time strings. Formats from various databases and Internet sources can be handled.