From 7f1f91b4f6281927595e027f67b9f91d00256211 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 13:25:23 -0800 Subject: [PATCH 01/13] First few updates --- docs/core/install/linux-snap-runtime.md | 2 +- docs/core/install/macos-notarization-issues.md | 2 +- docs/standard/datetime/timeprovider-overview.md | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/core/install/linux-snap-runtime.md b/docs/core/install/linux-snap-runtime.md index a012e1f280a56..c6e98f82ff6b5 100644 --- a/docs/core/install/linux-snap-runtime.md +++ b/docs/core/install/linux-snap-runtime.md @@ -3,7 +3,7 @@ title: Install .NET Runtime on Linux with Snap description: Learn about how to install the .NET Runtime snap package. Canonical maintains and supports .NET-related snap packages. author: adegeo ms.author: adegeo -ms.date: 12/13/2024 +ms.date: 01/16/2026 ms.topic: install-set-up-deploy ms.custom: linux-related-content #customer intent: As a Linux user, I want to install .NET Runtime through Snap. diff --git a/docs/core/install/macos-notarization-issues.md b/docs/core/install/macos-notarization-issues.md index 0ab0881543138..a9e247233ab03 100644 --- a/docs/core/install/macos-notarization-issues.md +++ b/docs/core/install/macos-notarization-issues.md @@ -3,7 +3,7 @@ title: Working with macOS Catalina Notarization description: How to handle notarization and certificate problems with macOS when you install the .NET runtime, SDK, and apps built with .NET. author: adegeo ms.author: adegeo -ms.date: 11/11/2024 +ms.date: 01/16/2026 --- # macOS Catalina Notarization and the impact on .NET downloads and projects diff --git a/docs/standard/datetime/timeprovider-overview.md b/docs/standard/datetime/timeprovider-overview.md index c105d1a2bdec0..4ed27acb68f39 100644 --- a/docs/standard/datetime/timeprovider-overview.md +++ b/docs/standard/datetime/timeprovider-overview.md @@ -1,7 +1,7 @@ --- title: What is the TimeProvider class description: Learn about the TimeProvider class in .NET and .NET Framework. TimeProvider provides an abstraction over time. -ms.date: 12/03/2024 +ms.date: 01/16/2026 ms.topic: overview dev_langs: - "csharp" @@ -16,6 +16,15 @@ helpviewer_keywords: is an abstraction of time that provides a point in time as a type. By using `TimeProvider`, you ensure that your code is testable and predictable. `TimeProvider` was introduced in .NET 8 and is also available for .NET Framework 4.7+ and .NET Standard 2.0 as a NuGet package. +`TimeProvider` is available on the following frameworks: + +| Framework | Notes | +|---|---| +| .NET 8+ | Included in the framework. | +| .NET 5 - .NET 7 | Provided in the [`Microsoft.Bcl.TimeProvider` NuGet package](https://www.nuget.org/packages/Microsoft.Bcl.TimeProvider). | +| .NET Framework 4.6.2+ | Provided in the [`Microsoft.Bcl.TimeProvider` NuGet package](https://www.nuget.org/packages/Microsoft.Bcl.TimeProvider). | +| .NET Standard 2.0 | Provided in the [`Microsoft.Bcl.TimeProvider` NuGet package](https://www.nuget.org/packages/Microsoft.Bcl.TimeProvider). | + The class defines the following capabilities: - Provides access to the date and time through and . From 24d7954aae0dee225001d4e200aba70594d4e6ba Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 14:40:42 -0800 Subject: [PATCH 02/13] Another est --- .../timeprovider-overview/csharp/myproject.csproj | 2 +- .../timeprovider-overview/vb/myproject.vbproj | 2 +- docs/standard/datetime/timeprovider-overview.md | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/standard/datetime/snippets/timeprovider-overview/csharp/myproject.csproj b/docs/standard/datetime/snippets/timeprovider-overview/csharp/myproject.csproj index 694035b3acd5f..dfb40caafcf9a 100644 --- a/docs/standard/datetime/snippets/timeprovider-overview/csharp/myproject.csproj +++ b/docs/standard/datetime/snippets/timeprovider-overview/csharp/myproject.csproj @@ -2,7 +2,7 @@ Exe - net9.0 + net10.0 enable enable diff --git a/docs/standard/datetime/snippets/timeprovider-overview/vb/myproject.vbproj b/docs/standard/datetime/snippets/timeprovider-overview/vb/myproject.vbproj index cacdff353fc16..1309fca0c98e9 100644 --- a/docs/standard/datetime/snippets/timeprovider-overview/vb/myproject.vbproj +++ b/docs/standard/datetime/snippets/timeprovider-overview/vb/myproject.vbproj @@ -3,7 +3,7 @@ Exe ExampleProject - net9.0 + net10.0 diff --git a/docs/standard/datetime/timeprovider-overview.md b/docs/standard/datetime/timeprovider-overview.md index 4ed27acb68f39..20f53eb1af750 100644 --- a/docs/standard/datetime/timeprovider-overview.md +++ b/docs/standard/datetime/timeprovider-overview.md @@ -37,16 +37,16 @@ The class defines the following capabilities: .NET provides an implementation of through the property, with the following characteristics: -- Date and time are calculated with and . +- Date and time are calculated by using and . - Timestamps are provided by . - Timers are implemented through an internal class and exposed as . -The following example demonstrates using to get the current date and time: +The following example shows how to use to get the current date and time: :::code language="csharp" source="./snippets/timeprovider-overview/csharp/Program.cs" id="GetLocal"::: :::code language="vb" source="./snippets/timeprovider-overview/vb/Program.vb" id="GetLocal"::: -The following example demonstrates capturing elapsed time with : +The following example shows how to capture elapsed time by using : :::code language="csharp" source="./snippets/timeprovider-overview/csharp/Program.cs" id="Timestamp"::: :::code language="vb" source="./snippets/timeprovider-overview/vb/Program.vb" id="Timestamp"::: @@ -63,12 +63,12 @@ The following list describes some of the capabilities of the and override members to control how time is provided. For example, the following class only provides a single date, the date of the moon landing: +While [FakeTimeProvider](#faketimeprovider-implementation) covers most scenarios that require predictability with time, you can still provide your own implementation. Create a new class that derives from and override members to control how time is provided. For example, the following class only provides a single date, the date of the moon landing: :::code language="csharp" source="./snippets/timeprovider-overview/csharp/MoonLandingTimeProviderPST.cs" id="CustomProvider"::: :::code language="vb" source="./snippets/timeprovider-overview/vb/MoonLandingTimeProviderPST.vb" id="CustomProvider"::: -If code using this class calls `MoonLandingTimeProviderPST.GetUtcNow`, the date of the moon landing in UTC is returned. If `MoonLandingTimeProviderPST.GetLocalNow` is called, the base class applies `MoonLandingTimeProviderPST.LocalTimeZone` to `GetUtcNow` and returns the moon landing date and time in the PST timezone. +If code using this class calls `MoonLandingTimeProviderPST.GetUtcNow`, the date of the moon landing in UTC is returned. If `MoonLandingTimeProviderPST.GetLocalNow` is called, the base class applies `MoonLandingTimeProviderPST.LocalTimeZone` to `GetUtcNow` and returns the moon landing date and time in the PST time zone. To demonstrate the usefulness of controlling time, consider the following example. Let's say that you're writing a calendar app that sends a greeting to the user when the app is first opened each day. The app says a special greeting when the current day has an event associated with it, such as the anniversary of the moon landing. @@ -89,7 +89,7 @@ And unit tests can be written to test specific scenarios, such as testing the an ## Use with .NET -Starting with .NET 8, the class is provided by the runtime library. Older versions of .NET or libraries targeting .NET Standard 2.0, must reference the [**Microsoft.Bcl.TimeProvider** NuGet package](https://www.nuget.org/packages/Microsoft.Bcl.TimeProvider/). +Starting with .NET 8, the runtime library provides the class. Older versions of .NET or libraries targeting .NET Standard 2.0 must reference the [`Microsoft.Bcl.TimeProvider` NuGet package](https://www.nuget.org/packages/Microsoft.Bcl.TimeProvider/). The following methods related to asynchronous programming work with `TimeProvider`: @@ -101,7 +101,7 @@ The following methods related to asynchronous programming work with `TimeProvide ## Use with .NET Framework - is implemented by the [**Microsoft.Bcl.TimeProvider** NuGet package](https://www.nuget.org/packages/Microsoft.Bcl.TimeProvider/). +The [`Microsoft.Bcl.TimeProvider` NuGet package](https://www.nuget.org/packages/Microsoft.Bcl.TimeProvider/) implements . Support for working with `TimeProvider` in asynchronous programming scenarios was added through the following extension methods: From 4c1009712d8f85d37eb03ef135e21122b06f9388 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 15:33:40 -0800 Subject: [PATCH 03/13] Update date/time article; move snippets; freshness --- docs/standard/base-types/parsing-datetime.md | 133 ++++++++++++--- .../parsing-datetime/csharp/Program.cs | 153 ++++++++++++++++++ .../csharp/parsing-datetime.csproj | 4 +- .../snippets/parsing-datetime/vb/Program.vb | 139 ++++++++++++++++ .../vb/parsing-datetime.vbproj | 9 ++ .../datetime/how-to-use-dateonly-timeonly.md | 2 +- .../csharp/dateonlytimeonly.csproj | 2 +- .../vb/dateonlytimeonly.vbproj | 2 +- .../csharp/how-to/conversions/Program.cs | 12 -- .../how-to/conversions/StringToDateTime.cs | 75 --------- .../visualbasic/how-to/conversions/Program.vb | 62 ------- .../how-to/conversions/conversions.vbproj | 8 - 12 files changed, 413 insertions(+), 188 deletions(-) create mode 100644 docs/standard/base-types/snippets/parsing-datetime/csharp/Program.cs rename samples/snippets/csharp/how-to/conversions/conversions.csproj => docs/standard/base-types/snippets/parsing-datetime/csharp/parsing-datetime.csproj (70%) create mode 100644 docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb create mode 100644 docs/standard/base-types/snippets/parsing-datetime/vb/parsing-datetime.vbproj delete mode 100644 samples/snippets/csharp/how-to/conversions/Program.cs delete mode 100644 samples/snippets/csharp/how-to/conversions/StringToDateTime.cs delete mode 100644 samples/snippets/visualbasic/how-to/conversions/Program.vb delete mode 100644 samples/snippets/visualbasic/how-to/conversions/conversions.vbproj diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index f952034fc02a9..246862fd9b8be 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -1,8 +1,9 @@ --- -title: Convert strings to DateTime -description: "Learn techniques to parse strings that represent dates and times to create a DateTime from the date and time string." -ms.date: 07/08/2022 +title: Parse date and time strings +description: "Learn techniques to parse strings that represent dates and times to create DateTime, DateOnly, and TimeOnly objects from string representations." +ms.date: 01/16/2026 ms.custom: devdivchpfy22 +ai-usage: ai-assisted dev_langs: - "csharp" - "vb" @@ -13,21 +14,49 @@ helpviewer_keywords: - "enumerations [.NET], parsing strings" - "base types, parsing strings" - "DateTime object" + - "DateOnly structure" + - "TimeOnly structure" - "time strings" --- # Parse date and time strings in .NET -Parsing strings to convert them to objects requires you to specify information about how the dates and times are represented as text. Different cultures use different orders for day, month, and year. Some time representations use a 24-hour clock, others specify "AM" and "PM." Some applications need only the date. Others need only the time. Still others need to specify both the date and time. The methods that convert strings to objects enable you to provide detailed information about the formats you expect and the elements of a date and time your application needs. There are three subtasks to correctly converting text into a : +.NET provides several types for working with date and time data, each optimized for different scenarios: -1. You must specify the expected format of the text representing a date and time. -1. You can specify the culture for the format of a date time. -1. You can specify how missing components in the text representation are set in the date and time. +- **** - Represents a date and time together, ideal when you need both components or when working with legacy code. +- **** (not available in .NET Framework) - Represents only a date without time information, perfect for birthdays, anniversaries, or business dates. +- **** (not available in .NET Framework) - Represents only a time without date information, ideal for schedules, alarms, or recurring daily events. -The and methods convert many common representations of a date and time. The and methods convert a string representation that conforms to the pattern specified by a date and time format string. For more information, see the articles on [standard date and time format strings](standard-date-and-time-format-strings.md) and [custom date and time format strings](custom-date-and-time-format-strings.md). +Each type provides parsing methods that convert strings to their respective objects, with different levels of flexibility and control over the parsing process. -The current object provides more control over how text should be interpreted as a date and time. Properties of a describe the date and time separators, the names of months, days, and eras, and the format for the "AM" and "PM" designations. The returned by has a property that represents the current culture. If you want a specific culture or custom settings, you specify the parameter of a parsing method. For the parameter, specify a object, which represents a culture, or a object. +## Common parsing concepts -The text representing a date or time might be missing some information. For example, most people would assume the date "March 12" represents the current year. Similarly, "March 2018" represents the month of March in the year 2018. Text representing time often does only include hours, minutes, and an AM/PM designation. Parsing methods handle this missing information by using reasonable defaults: +All three date and time types share similar parsing approaches: + +- **Parse methods** - Convert many common string representations using current culture or specified culture settings +- **ParseExact methods** - Convert strings that conform to specific format patterns, providing precise control over expected formats +- **Culture and format providers** - Control how text is interpreted based on cultural conventions for date and time representation +- **Format strings** - Define patterns for parsing using standard or custom format specifiers + +Different cultures use different orders for day, month, and year. Some time representations use a 24-hour clock, others specify "AM" and "PM." The parsing methods handle these variations through culture-specific formatting rules. + +The object provides control over how text should be interpreted. Properties describe the date and time separators, names of months, days, and eras, and the format for "AM" and "PM" designations. You can specify culture through the parameter using a object or a object. + +For more information about format patterns, see [standard date and time format strings](standard-date-and-time-format-strings.md) and [custom date and time format strings](custom-date-and-time-format-strings.md). + +> [!IMPORTANT] +> and types aren't available for .NET Framework. + +## DateTime parsing + + represents both date and time components together. When parsing strings to DateTime objects, you need to consider several DateTime-specific aspects: + +1. **Missing information handling** - DateTime uses defaults when parts are missing from the input string +2. **Time zone and UTC offset support** - DateTime can represent local, UTC, or unspecified time zones +3. **Combined date and time parsing** - Must handle both date and time components in a single operation + +### Missing information handling + +The text representing a date or time might be missing some information. For example, most people would assume the date "March 12" represents the current year. Similarly, "March 2018" represents the month of March in the year 2018. Text representing time often includes only hours, minutes, and an AM/PM designation. DateTime parsing methods handle this missing information by using reasonable defaults: - When only the time is present, the date portion uses the current date. - When only the date is present, the time portion is midnight. @@ -38,42 +67,93 @@ If the date is present in the string, it must include the month and one of the d You can specify the constant to override these defaults. When you use that constant, any missing year, month, or day properties are set to the value `1`. The [last example](#styles-example) using demonstrates this behavior. -In addition to a date and a time component, the string representation of a date and time can include an offset that indicates how much the time differs from Coordinated Universal Time (UTC). For example, the string "2/14/2007 5:32:00 -7:00" defines a time that is seven hours earlier than UTC. If an offset is omitted from the string representation of a time, parsing returns a object with its property set to . If an offset is specified, parsing returns a object with its property set to . Its value is also adjusted to the local time zone of your machine. You can modify this behavior by using a value with the parsing method. +### UTC offset and time zone handling -The format provider is also used to interpret an ambiguous numeric date. It's unclear which components of the date represented by the string "02/03/04" are the month, day, and year. The components are interpreted according to the order of similar date formats in the format provider. +In addition to a date and a time component, the string representation of a date and time can include an offset that indicates how much the time differs from Coordinated Universal Time (UTC). For example, the string "2/14/2007 5:32:00 -7:00" defines a time that's seven hours earlier than UTC. If an offset is omitted from the string representation of a time, parsing returns a object with its property set to . If an offset is specified, parsing returns a object with its property set to . Its value is also adjusted to the local time zone of your machine. You can modify this behavior by using a value with the parsing method. -## Parse +### Ambiguous date handling + +The format provider is also used to interpret an ambiguous numeric date. It's unclear which components of the date represented by the string "02/03/04" are the month, day, and year. The components are interpreted according to the order of similar date formats in the format provider. -The following example illustrates the use of the method to convert a `string` into a . This example uses the culture associated with the current thread. If the associated with the current culture can't parse the input string, a is thrown. +### DateTime.Parse -> [!NOTE] -> These examples are available in the GitHub docs repo for both [C#](https://github.com/dotnet/docs/tree/main/samples/snippets/csharp/how-to/conversions) and [Visual Basic](https://github.com/dotnet/docs/tree/main/samples/snippets/visualbasic/how-to/conversions). +The following example shows the use of the method to convert a `string` into a . This example uses the culture associated with the current thread. If the associated with the current culture can't parse the input string, a is thrown. -[!code-csharp[Parsing.DateAndTime#1](../../../samples/snippets/csharp/how-to/conversions/StringToDateTime.cs#1)] -[!code-vb[Parsing.DateAndTime#1](../../../samples/snippets/visualbasic/how-to/conversions/Program.vb#1)] +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse" lang="vb"::: You can also explicitly define the culture whose formatting conventions are used when you parse a string. You specify one of the standard objects returned by the property. The following example uses a format provider to parse a German string into a . It creates a representing the `de-DE` culture. That `CultureInfo` object ensures successful parsing of this particular string. This process precludes whatever setting is in the of the . -[!code-csharp[Parsing.DateAndTime#2](../../../samples/snippets/csharp/how-to/conversions/StringToDateTime.cs#2)] -[!code-vb[Parsing.DateAndTime#2](../../../samples/snippets/visualbasic/how-to/conversions/Program.vb#2)] +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" lang="vb"::: However, you can use overloads of the method to specify custom format providers. The method doesn't support parsing non-standard formats. To parse a date and time expressed in a non-standard format, use the method instead. The following example uses the enumeration to specify that the current date and time information shouldn't be added to the for unspecified fields. -[!code-csharp[Parsing.DateAndTime#3](../../../samples/snippets/csharp/how-to/conversions/StringToDateTime.cs#3)] -[!code-vb[Parsing.DateAndTime#3](../../../samples/snippets/visualbasic/how-to/conversions/Program.vb#3)] +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-nodefault" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-nodefault" lang="vb"::: -## ParseExact +### DateTime.ParseExact The method converts a string to a object if it conforms to one of the specified string patterns. When a string that isn't one of the forms specified is passed to this method, a is thrown. You can specify one of the standard date and time format specifiers or a combination of the custom format specifiers. Using the custom format specifiers, it's possible for you to construct a custom recognition string. For an explanation of the specifiers, see the articles on [standard date and time format strings](standard-date-and-time-format-strings.md) and [custom date and time format strings](custom-date-and-time-format-strings.md). In the following example, the method is passed a string object to parse, followed by a format specifier, followed by a object. This method can only parse strings that follow the long date pattern in the `en-US` culture. -[!code-csharp[Parsing.DateAndTime#4](../../../samples/snippets/csharp/how-to/conversions/StringToDateTime.cs#4)] -[!code-vb[Parsing.DateAndTime#4](../../../samples/snippets/visualbasic/how-to/conversions/Program.vb#4)] +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parseexact" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parseexact" lang="vb"::: + +Each overload of the and methods also has an parameter that provides culture-specific information about the formatting of the string. The object is a object that represents a standard culture or a object that's returned by the property. also uses an additional string or string array argument that defines one or more custom date and time formats. + +## DateOnly parsing + +The structure represents only a date without time information, making it perfect for scenarios like birthdays, anniversaries, or business dates. Since it has no time component, it represents a date from the start of the day to the end of the day. + +`DateOnly` has several advantages over using `DateTime` for date-only scenarios: + +- The `DateTime` structure might roll into the previous or next day if it's offset by a time zone. `DateOnly` can't be offset by a time zone, and it always represents the date that was set. +- Serializing a `DateOnly` includes less data than `DateTime`. +- When code interacts with a database, such as SQL Server, whole dates are generally stored as the `date` data type, which doesn't include a time. `DateOnly` matches the database type better. + +### DateOnly.Parse + +The method converts common date string representations to a object. The method accepts various formats and uses the current culture or a specified culture for parsing. + +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parse" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parse" lang="vb"::: + +### DateOnly.ParseExact + +The method provides precise control over the expected format of the input string. Use this method when you know the exact format of the date string and want to ensure strict parsing. + +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parseexact" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parseexact" lang="vb"::: + +The `ParseExact` method accepts either a single format string or an array of format strings, allowing you to parse dates that might come in multiple acceptable formats. + +## TimeOnly parsing + +The structure represents a time-of-day value, such as a daily alarm clock or what time you eat lunch each day. `TimeOnly` is limited to the range of **00:00:00.0000000** - **23:59:59.9999999**, a specific time of day. + +`TimeOnly` solves several problems that existed when using other types for time-only scenarios: + +- `TimeSpan` represents elapsed time and can be negative or exceed 24 hours, making it unsuitable for representing a specific time of day. +- Using `DateTime` for a time of day requires an arbitrary date, which can lead to unexpected behavior when performing calculations. +- `TimeOnly` naturally handles 24-hour rollover when adding or subtracting time values. + +### TimeOnly.Parse + +The method converts common time string representations to a object. The method accepts various formats including 12-hour and 24-hour notation. + +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parse" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parse" lang="vb"::: + +### TimeOnly.ParseExact + +The method provides precise control over the expected format of the input time string. Use this method when you know the exact format and want to ensure strict parsing. -Each overload of the and methods also has an parameter that provides culture-specific information about the formatting of the string. The object is a object that represents a standard culture or a object that is returned by the property. also uses an additional string or string array argument that defines one or more custom date and time formats. +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parseexact" lang="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parseexact" lang="vb"::: ## See also @@ -82,3 +162,4 @@ Each overload of the and + static void DateTimeParseExample() + { + // Parse common date and time formats using current culture + var dateTime1 = DateTime.Parse("1/15/2025 3:30 PM"); + var dateTime2 = DateTime.Parse("January 15, 2025"); + var dateTime3 = DateTime.Parse("15:30:45"); + + Console.WriteLine($"Parsed: {dateTime1}"); + Console.WriteLine($"Parsed: {dateTime2}"); + Console.WriteLine($"Parsed: {dateTime3}"); + + // Parse with specific culture + var germanDate = DateTime.Parse("15.01.2025", new CultureInfo("de-DE")); + Console.WriteLine($"German date parsed: {germanDate}"); + } + // + + // + static void DateTimeParseExactExample() + { + // Parse exact format + var exactDate = DateTime.ParseExact("2025-01-15T14:30:00", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture); + Console.WriteLine($"Exact parse: {exactDate}"); + + // Parse with custom format + var customDate = DateTime.ParseExact("15/Jan/2025 2:30 PM", "dd/MMM/yyyy h:mm tt", CultureInfo.InvariantCulture); + Console.WriteLine($"Custom format: {customDate}"); + } + // + + // + static void DateTimeParseGermanExample() + { + var cultureInfo = new CultureInfo("de-DE"); + string dateString = "12 Juni 2008"; + var dateTime = DateTime.Parse(dateString, cultureInfo); + Console.WriteLine(dateTime); + // The example displays the following output: + // 6/12/2008 00:00:00 + } + // + + // + static void DateTimeParseNoDefaultExample() + { + var cultureInfo = new CultureInfo("de-DE"); + string dateString = "12 Juni 2008"; + var dateTime = DateTime.Parse(dateString, cultureInfo, + DateTimeStyles.NoCurrentDateDefault); + Console.WriteLine(dateTime); + // The example displays the following output if the current culture is en-US: + // 6/12/2008 00:00:00 + } + // + + // + static void DateOnlyParseExample() + { + // Parse common date formats + var date1 = DateOnly.Parse("1/15/2025"); + var date2 = DateOnly.Parse("January 15, 2025", CultureInfo.InvariantCulture); + var date3 = DateOnly.Parse("2025-01-15"); + + Console.WriteLine($"Parsed date: {date1}"); + Console.WriteLine($"Parsed date: {date2.ToString("D")}"); // Long date format + Console.WriteLine($"Parsed date: {date3.ToString("yyyy-MM-dd")}"); + + // Parse with specific culture + var germanDate = DateOnly.Parse("15.01.2025", new CultureInfo("de-DE")); + Console.WriteLine($"German date: {germanDate}"); + } + // + + // + static void DateOnlyParseExactExample() + { + // Parse exact format + var exactDate = DateOnly.ParseExact("21 Oct 2015", "dd MMM yyyy", CultureInfo.InvariantCulture); + Console.WriteLine($"Exact date: {exactDate}"); + + // Parse ISO format + var isoDate = DateOnly.ParseExact("2025-01-15", "yyyy-MM-dd", CultureInfo.InvariantCulture); + Console.WriteLine($"ISO date: {isoDate}"); + + // Parse with multiple possible formats + string[] formats = { "MM/dd/yyyy", "M/d/yyyy", "dd/MM/yyyy" }; + var flexibleDate = DateOnly.ParseExact("1/15/2025", formats, CultureInfo.InvariantCulture, DateTimeStyles.None); + Console.WriteLine($"Flexible parse: {flexibleDate}"); + } + // + + // + static void TimeOnlyParseExample() + { + // Parse common time formats + var time1 = TimeOnly.Parse("14:30:15"); + var time2 = TimeOnly.Parse("2:30 PM", CultureInfo.InvariantCulture); + var time3 = TimeOnly.Parse("17:45"); + + Console.WriteLine($"Parsed time: {time1}"); + Console.WriteLine($"Parsed time: {time2.ToString("t")}"); // Short time format + Console.WriteLine($"Parsed time: {time3.ToString("HH:mm")}"); + + // Parse with milliseconds + var preciseTime = TimeOnly.Parse("14:30:15.123"); + Console.WriteLine($"Precise time: {preciseTime.ToString("HH:mm:ss.fff")}"); + } + // + + // + static void TimeOnlyParseExactExample() + { + // Parse exact format + var exactTime = TimeOnly.ParseExact("5:00 pm", "h:mm tt", CultureInfo.InvariantCulture); + Console.WriteLine($"Exact time: {exactTime}"); + + // Parse 24-hour format + var militaryTime = TimeOnly.ParseExact("17:30:25", "HH:mm:ss", CultureInfo.InvariantCulture); + Console.WriteLine($"Military time: {militaryTime}"); + + // Parse with multiple possible formats + string[] timeFormats = { "h:mm tt", "HH:mm", "H:mm" }; + var flexibleTime = TimeOnly.ParseExact("2:30 PM", timeFormats, CultureInfo.InvariantCulture, DateTimeStyles.None); + Console.WriteLine($"Flexible time parse: {flexibleTime}"); + } + // + } +} \ No newline at end of file diff --git a/samples/snippets/csharp/how-to/conversions/conversions.csproj b/docs/standard/base-types/snippets/parsing-datetime/csharp/parsing-datetime.csproj similarity index 70% rename from samples/snippets/csharp/how-to/conversions/conversions.csproj rename to docs/standard/base-types/snippets/parsing-datetime/csharp/parsing-datetime.csproj index af31fefa23aa5..a5bdb8bb0cc69 100644 --- a/samples/snippets/csharp/how-to/conversions/conversions.csproj +++ b/docs/standard/base-types/snippets/parsing-datetime/csharp/parsing-datetime.csproj @@ -2,8 +2,8 @@ Exe - net8.0 + net10.0 enable - + \ No newline at end of file diff --git a/docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb b/docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb new file mode 100644 index 0000000000000..b01faa2656196 --- /dev/null +++ b/docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb @@ -0,0 +1,139 @@ +Imports System.Globalization + +Module Program + Sub Main(args As String()) + Console.WriteLine("=== DateTime Parsing Examples ===") + DateTimeParseExample() + DateTimeParseGermanExample() + DateTimeParseNoDefaultExample() + DateTimeParseExactExample() + + Console.WriteLine(Environment.NewLine & "=== DateOnly Parsing Examples ===") + DateOnlyParseExample() + DateOnlyParseExactExample() + + Console.WriteLine(Environment.NewLine & "=== TimeOnly Parsing Examples ===") + TimeOnlyParseExample() + TimeOnlyParseExactExample() + End Sub + + ' + Sub DateTimeParseExample() + ' Parse common date and time formats using current culture + Dim dateTime1 = DateTime.Parse("1/15/2025 3:30 PM") + Dim dateTime2 = DateTime.Parse("January 15, 2025") + Dim dateTime3 = DateTime.Parse("15:30:45") + + Console.WriteLine($"Parsed: {dateTime1}") + Console.WriteLine($"Parsed: {dateTime2}") + Console.WriteLine($"Parsed: {dateTime3}") + + ' Parse with specific culture + Dim germanDate = DateTime.Parse("15.01.2025", New CultureInfo("de-DE")) + Console.WriteLine($"German date parsed: {germanDate}") + End Sub + ' + + ' + Sub DateTimeParseExactExample() + ' Parse exact format + Dim exactDate = DateTime.ParseExact("2025-01-15T14:30:00", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture) + Console.WriteLine($"Exact parse: {exactDate}") + + ' Parse with custom format + Dim customDate = DateTime.ParseExact("15/Jan/2025 2:30 PM", "dd/MMM/yyyy h:mm tt", CultureInfo.InvariantCulture) + Console.WriteLine($"Custom format: {customDate}") + End Sub + ' + + ' + Sub DateTimeParseGermanExample() + Dim MyCultureInfo As New CultureInfo("de-DE") + Dim MyString As String = "12 Juni 2008" + Dim MyDateTime As DateTime = DateTime.Parse(MyString, MyCultureInfo) + Console.WriteLine(MyDateTime) + ' The example displays the following output: + ' 6/12/2008 00:00:00 + End Sub + ' + + ' + Sub DateTimeParseNoDefaultExample() + Dim MyCultureInfo As New CultureInfo("de-DE") + Dim MyString As String = "12 Juni 2008" + Dim MyDateTime As DateTime = DateTime.Parse(MyString, MyCultureInfo, + DateTimeStyles.NoCurrentDateDefault) + Console.WriteLine(MyDateTime) + ' The example displays the following output if the current culture is en-US: + ' 6/12/2008 00:00:00 + End Sub + ' + + ' + Sub DateOnlyParseExample() + ' Parse common date formats + Dim date1 = DateOnly.Parse("1/15/2025") + Dim date2 = DateOnly.Parse("January 15, 2025", CultureInfo.InvariantCulture) + Dim date3 = DateOnly.Parse("2025-01-15") + + Console.WriteLine($"Parsed date: {date1}") + Console.WriteLine($"Parsed date: {date2.ToString("D")}") ' Long date format + Console.WriteLine($"Parsed date: {date3.ToString("yyyy-MM-dd")}") + + ' Parse with specific culture + Dim germanDate = DateOnly.Parse("15.01.2025", New CultureInfo("de-DE")) + Console.WriteLine($"German date: {germanDate}") + End Sub + ' + + ' + Sub DateOnlyParseExactExample() + ' Parse exact format + Dim exactDate = DateOnly.ParseExact("21 Oct 2015", "dd MMM yyyy", CultureInfo.InvariantCulture) + Console.WriteLine($"Exact date: {exactDate}") + + ' Parse ISO format + Dim isoDate = DateOnly.ParseExact("2025-01-15", "yyyy-MM-dd", CultureInfo.InvariantCulture) + Console.WriteLine($"ISO date: {isoDate}") + + ' Parse with multiple possible formats + Dim formats() As String = {"MM/dd/yyyy", "M/d/yyyy", "dd/MM/yyyy"} + Dim flexibleDate = DateOnly.ParseExact("1/15/2025", formats, CultureInfo.InvariantCulture, DateTimeStyles.None) + Console.WriteLine($"Flexible parse: {flexibleDate}") + End Sub + ' + + ' + Sub TimeOnlyParseExample() + ' Parse common time formats + Dim time1 = TimeOnly.Parse("14:30:15") + Dim time2 = TimeOnly.Parse("2:30 PM", CultureInfo.InvariantCulture) + Dim time3 = TimeOnly.Parse("17:45") + + Console.WriteLine($"Parsed time: {time1}") + Console.WriteLine($"Parsed time: {time2.ToString("t")}") ' Short time format + Console.WriteLine($"Parsed time: {time3.ToString("HH:mm")}") + + ' Parse with milliseconds + Dim preciseTime = TimeOnly.Parse("14:30:15.123") + Console.WriteLine($"Precise time: {preciseTime.ToString("HH:mm:ss.fff")}") + End Sub + ' + + ' + Sub TimeOnlyParseExactExample() + ' Parse exact format + Dim exactTime = TimeOnly.ParseExact("5:00 pm", "h:mm tt", CultureInfo.InvariantCulture) + Console.WriteLine($"Exact time: {exactTime}") + + ' Parse 24-hour format + Dim militaryTime = TimeOnly.ParseExact("17:30:25", "HH:mm:ss", CultureInfo.InvariantCulture) + Console.WriteLine($"Military time: {militaryTime}") + + ' Parse with multiple possible formats + Dim timeFormats() As String = {"h:mm tt", "HH:mm", "H:mm"} + Dim flexibleTime = TimeOnly.ParseExact("2:30 PM", timeFormats, CultureInfo.InvariantCulture, DateTimeStyles.None) + Console.WriteLine($"Flexible time parse: {flexibleTime}") + End Sub + ' +End Module \ No newline at end of file diff --git a/docs/standard/base-types/snippets/parsing-datetime/vb/parsing-datetime.vbproj b/docs/standard/base-types/snippets/parsing-datetime/vb/parsing-datetime.vbproj new file mode 100644 index 0000000000000..773a87394dd64 --- /dev/null +++ b/docs/standard/base-types/snippets/parsing-datetime/vb/parsing-datetime.vbproj @@ -0,0 +1,9 @@ + + + + Exe + net10.0 + parsing_datetime + + + \ No newline at end of file diff --git a/docs/standard/datetime/how-to-use-dateonly-timeonly.md b/docs/standard/datetime/how-to-use-dateonly-timeonly.md index 8f1cfec99f39a..e4ebb68226a66 100644 --- a/docs/standard/datetime/how-to-use-dateonly-timeonly.md +++ b/docs/standard/datetime/how-to-use-dateonly-timeonly.md @@ -1,7 +1,7 @@ --- title: How to use DateOnly and TimeOnly description: Learn about the DateOnly and TimeOnly structures in .NET. -ms.date: 12/05/2024 +ms.date: 01/16/2026 dev_langs: - "csharp" - "vb" diff --git a/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/csharp/dateonlytimeonly.csproj b/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/csharp/dateonlytimeonly.csproj index 91b464afeacc1..dfb40caafcf9a 100644 --- a/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/csharp/dateonlytimeonly.csproj +++ b/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/csharp/dateonlytimeonly.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net10.0 enable enable diff --git a/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/vb/dateonlytimeonly.vbproj b/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/vb/dateonlytimeonly.vbproj index 92807d1660a18..378c6932de99a 100644 --- a/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/vb/dateonlytimeonly.vbproj +++ b/docs/standard/datetime/snippets/how-to-use-dateonly-timeonly/vb/dateonlytimeonly.vbproj @@ -3,7 +3,7 @@ Exe dateonlytimeonly - net8.0 + net10.0 diff --git a/samples/snippets/csharp/how-to/conversions/Program.cs b/samples/snippets/csharp/how-to/conversions/Program.cs deleted file mode 100644 index 9d8454cff713c..0000000000000 --- a/samples/snippets/csharp/how-to/conversions/Program.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace conversions -{ - class Program - { - static void Main(string[] args) - { - StringToDateTime.Examples(); - } - } -} diff --git a/samples/snippets/csharp/how-to/conversions/StringToDateTime.cs b/samples/snippets/csharp/how-to/conversions/StringToDateTime.cs deleted file mode 100644 index 77f134f3bf17b..0000000000000 --- a/samples/snippets/csharp/how-to/conversions/StringToDateTime.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Globalization; - -namespace conversions -{ - public static class StringToDateTime - { - public static void Examples() - { - FirstExample(); - GlobalExample(); - NoDefaultToCurrentDateTime(); - ParseExactExample(); - } - - static void FirstExample() - { - // - string dateInput = "Jan 1, 2009"; - var parsedDate = DateTime.Parse(dateInput); - Console.WriteLine(parsedDate); - // Displays the following output on a system whose culture is en-US: - // 1/1/2009 00:00:00 - // - } - - static void GlobalExample() - { - // - var cultureInfo = new CultureInfo("de-DE"); - string dateString = "12 Juni 2008"; - var dateTime = DateTime.Parse(dateString, cultureInfo); - Console.WriteLine(dateTime); - // The example displays the following output: - // 6/12/2008 00:00:00 - // - } - - static void NoDefaultToCurrentDateTime() - { - // - var cultureInfo = new CultureInfo("de-DE"); - string dateString = "12 Juni 2008"; - var dateTime = DateTime.Parse(dateString, cultureInfo, - DateTimeStyles.NoCurrentDateDefault); - Console.WriteLine(dateTime); - // The example displays the following output if the current culture is en-US: - // 6/12/2008 00:00:00 - // - } - - static void ParseExactExample() - { - // - var cultureInfo = new CultureInfo("en-US"); - string[] dateStrings = { " Friday, April 10, 2009", "Friday, April 10, 2009" }; - foreach (string dateString in dateStrings) - { - try - { - var dateTime = DateTime.ParseExact(dateString, "D", cultureInfo); - Console.WriteLine(dateTime); - } - catch (FormatException) - { - Console.WriteLine($"Unable to parse '{dateString}'"); - } - } - // The example displays the following output: - // Unable to parse ' Friday, April 10, 2009' - // 4/10/2009 00:00:00 - // - } - } -} \ No newline at end of file diff --git a/samples/snippets/visualbasic/how-to/conversions/Program.vb b/samples/snippets/visualbasic/how-to/conversions/Program.vb deleted file mode 100644 index 44d81451fcdf0..0000000000000 --- a/samples/snippets/visualbasic/how-to/conversions/Program.vb +++ /dev/null @@ -1,62 +0,0 @@ -Imports System.Globalization - -Module Program - Sub Main(args As String()) - FirstExample() - GlobalExample() - DefaultValueExample() - ParseExactExample() - End Sub - - Sub FirstExample() - ' - Dim MyString As String = "Jan 1, 2009" - Dim MyDateTime As DateTime = DateTime.Parse(MyString) - Console.WriteLine(MyDateTime) - ' Displays the following output on a system whose culture is en-US: - ' 1/1/2009 00:00:00 - ' - End Sub - - Sub GlobalExample() - ' - Dim MyCultureInfo As New CultureInfo("de-DE") - Dim MyString As String = "12 Juni 2008" - Dim MyDateTime As DateTime = DateTime.Parse(MyString, MyCultureInfo) - Console.WriteLine(MyDateTime) - ' The example displays the following output: - ' 6/12/2008 00:00:00 - ' - End Sub - - Sub DefaultValueExample() - ' - Dim MyCultureInfo As New CultureInfo("de-DE") - Dim MyString As String = "12 Juni 2008" - Dim MyDateTime As DateTime = DateTime.Parse(MyString, MyCultureInfo, - DateTimeStyles.NoCurrentDateDefault) - Console.WriteLine(MyDateTime) - ' The example displays the following output if the current culture is en-US: - ' 6/12/2008 00:00:00 - ' - End Sub - - Sub ParseExactExample() - ' - Dim MyCultureInfo As New CultureInfo("en-US") - Dim MyString() As String = {" Friday, April 10, 2009", "Friday, April 10, 2009"} - For Each dateString As String In MyString - Try - Dim MyDateTime As DateTime = DateTime.ParseExact(dateString, "D", - MyCultureInfo) - Console.WriteLine(MyDateTime) - Catch e As FormatException - Console.WriteLine("Unable to parse '{0}'", dateString) - End Try - Next - ' The example displays the following output: - ' Unable to parse ' Friday, April 10, 2009' - ' 4/10/2009 00:00:00 - ' - End Sub -End Module diff --git a/samples/snippets/visualbasic/how-to/conversions/conversions.vbproj b/samples/snippets/visualbasic/how-to/conversions/conversions.vbproj deleted file mode 100644 index a269962b552f0..0000000000000 --- a/samples/snippets/visualbasic/how-to/conversions/conversions.vbproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - Exe - net8.0 - - - From cdb7e512d4097df274c525edbccab62ac4219e84 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 15:57:45 -0800 Subject: [PATCH 04/13] Fix copilot catches --- docs/standard/base-types/parsing-datetime.md | 2 +- docs/standard/datetime/timeprovider-overview.md | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index 246862fd9b8be..d7452d9b22edc 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -39,7 +39,7 @@ All three date and time types share similar parsing approaches: Different cultures use different orders for day, month, and year. Some time representations use a 24-hour clock, others specify "AM" and "PM." The parsing methods handle these variations through culture-specific formatting rules. -The object provides control over how text should be interpreted. Properties describe the date and time separators, names of months, days, and eras, and the format for "AM" and "PM" designations. You can specify culture through the parameter using a object or a object. +The object provides control over how text should be interpreted. Properties describe the date and time separators, names of months, days, eras, and the format for "AM" and "PM" designations. You can specify culture through the parameter using a object or a object. For more information about format patterns, see [standard date and time format strings](standard-date-and-time-format-strings.md) and [custom date and time format strings](custom-date-and-time-format-strings.md). diff --git a/docs/standard/datetime/timeprovider-overview.md b/docs/standard/datetime/timeprovider-overview.md index 20f53eb1af750..b3322485d324a 100644 --- a/docs/standard/datetime/timeprovider-overview.md +++ b/docs/standard/datetime/timeprovider-overview.md @@ -14,9 +14,7 @@ helpviewer_keywords: # What is TimeProvider? - is an abstraction of time that provides a point in time as a type. By using `TimeProvider`, you ensure that your code is testable and predictable. `TimeProvider` was introduced in .NET 8 and is also available for .NET Framework 4.7+ and .NET Standard 2.0 as a NuGet package. - -`TimeProvider` is available on the following frameworks: + is an abstraction of time that provides a point in time as a type. By using `TimeProvider`, you ensure that your code is testable and predictable. `TimeProvider` is available on the following frameworks: | Framework | Notes | |---|---| From ee09dc64b2f33b4cb96b538e3099dc35d28c5098 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 15:59:45 -0800 Subject: [PATCH 05/13] Fix periods --- docs/standard/base-types/parsing-datetime.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index d7452d9b22edc..d9de4a74ed41e 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -32,10 +32,10 @@ Each type provides parsing methods that convert strings to their respective obje All three date and time types share similar parsing approaches: -- **Parse methods** - Convert many common string representations using current culture or specified culture settings -- **ParseExact methods** - Convert strings that conform to specific format patterns, providing precise control over expected formats -- **Culture and format providers** - Control how text is interpreted based on cultural conventions for date and time representation -- **Format strings** - Define patterns for parsing using standard or custom format specifiers +- **Parse methods** - Convert many common string representations using current culture or specified culture settings. +- **ParseExact methods** - Convert strings that conform to specific format patterns, providing precise control over expected formats. +- **Culture and format providers** - Control how text is interpreted based on cultural conventions for date and time representation. +- **Format strings** - Define patterns for parsing using standard or custom format specifiers. Different cultures use different orders for day, month, and year. Some time representations use a 24-hour clock, others specify "AM" and "PM." The parsing methods handle these variations through culture-specific formatting rules. @@ -50,9 +50,9 @@ For more information about format patterns, see [standard date and time format s represents both date and time components together. When parsing strings to DateTime objects, you need to consider several DateTime-specific aspects: -1. **Missing information handling** - DateTime uses defaults when parts are missing from the input string -2. **Time zone and UTC offset support** - DateTime can represent local, UTC, or unspecified time zones -3. **Combined date and time parsing** - Must handle both date and time components in a single operation +1. **Missing information handling** - DateTime uses defaults when parts are missing from the input string. +2. **Time zone and UTC offset support** - DateTime can represent local, UTC, or unspecified time zones. +3. **Combined date and time parsing** - Must handle both date and time components in a single operation. ### Missing information handling From 632da3cc27b59d0b97a725d88f2865a067b6aa81 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 16:01:42 -0800 Subject: [PATCH 06/13] Fix DateTime mentions --- docs/standard/base-types/parsing-datetime.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index d9de4a74ed41e..108890fb4892a 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -48,15 +48,15 @@ For more information about format patterns, see [standard date and time format s ## DateTime parsing - represents both date and time components together. When parsing strings to DateTime objects, you need to consider several DateTime-specific aspects: + represents both date and time components together. When parsing strings to `DateTime` objects, you need to consider several `DateTime`-specific aspects: -1. **Missing information handling** - DateTime uses defaults when parts are missing from the input string. -2. **Time zone and UTC offset support** - DateTime can represent local, UTC, or unspecified time zones. +1. **Missing information handling** - `DateTime` uses defaults when parts are missing from the input string. +2. **Time zone and UTC offset support** - `DateTime` can represent local, UTC, or unspecified time zones. 3. **Combined date and time parsing** - Must handle both date and time components in a single operation. ### Missing information handling -The text representing a date or time might be missing some information. For example, most people would assume the date "March 12" represents the current year. Similarly, "March 2018" represents the month of March in the year 2018. Text representing time often includes only hours, minutes, and an AM/PM designation. DateTime parsing methods handle this missing information by using reasonable defaults: +The text representing a date or time might be missing some information. For example, most people would assume the date "March 12" represents the current year. Similarly, "March 2018" represents the month of March in the year 2018. Text representing time often includes only hours, minutes, and an AM/PM designation. `DateTime` parsing methods handle this missing information by using reasonable defaults: - When only the time is present, the date portion uses the current date. - When only the date is present, the time portion is midnight. From 9761e454ac956b6df1ec1ac6d5c4d11f5d3d770b Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 16:04:43 -0800 Subject: [PATCH 07/13] Trim up list --- docs/standard/base-types/parsing-datetime.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index 108890fb4892a..7d22ef93b915e 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -33,8 +33,7 @@ Each type provides parsing methods that convert strings to their respective obje All three date and time types share similar parsing approaches: - **Parse methods** - Convert many common string representations using current culture or specified culture settings. -- **ParseExact methods** - Convert strings that conform to specific format patterns, providing precise control over expected formats. -- **Culture and format providers** - Control how text is interpreted based on cultural conventions for date and time representation. +- **ParseExact methods** - Convert strings that conform to specific format patterns, providing precise control over expected formats, including culture settings. - **Format strings** - Define patterns for parsing using standard or custom format specifiers. Different cultures use different orders for day, month, and year. Some time representations use a 24-hour clock, others specify "AM" and "PM." The parsing methods handle these variations through culture-specific formatting rules. @@ -50,9 +49,9 @@ For more information about format patterns, see [standard date and time format s represents both date and time components together. When parsing strings to `DateTime` objects, you need to consider several `DateTime`-specific aspects: -1. **Missing information handling** - `DateTime` uses defaults when parts are missing from the input string. -2. **Time zone and UTC offset support** - `DateTime` can represent local, UTC, or unspecified time zones. -3. **Combined date and time parsing** - Must handle both date and time components in a single operation. +- **Missing information handling** - `DateTime` uses defaults when parts are missing from the input string. +- **Time zone and UTC offset support** - `DateTime` can represent local, UTC, or unspecified time zones. +- **Combined date and time parsing** - Must handle both date and time components in a single operation. ### Missing information handling From a373729f463dad10af3b7a9f77b788a7425e7ac2 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 16:06:08 -0800 Subject: [PATCH 08/13] Fix snippets --- docs/standard/base-types/parsing-datetime.md | 32 ++++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index 7d22ef93b915e..9c651ceab5221 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -78,20 +78,20 @@ The format provider is also used to interpret an ambiguous numeric date. It's un The following example shows the use of the method to convert a `string` into a . This example uses the culture associated with the current thread. If the associated with the current culture can't parse the input string, a is thrown. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse" language="vb"::: You can also explicitly define the culture whose formatting conventions are used when you parse a string. You specify one of the standard objects returned by the property. The following example uses a format provider to parse a German string into a . It creates a representing the `de-DE` culture. That `CultureInfo` object ensures successful parsing of this particular string. This process precludes whatever setting is in the of the . -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" language="vb"::: However, you can use overloads of the method to specify custom format providers. The method doesn't support parsing non-standard formats. To parse a date and time expressed in a non-standard format, use the method instead. The following example uses the enumeration to specify that the current date and time information shouldn't be added to the for unspecified fields. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-nodefault" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-nodefault" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-nodefault" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-nodefault" language="vb"::: ### DateTime.ParseExact @@ -99,8 +99,8 @@ The method con In the following example, the method is passed a string object to parse, followed by a format specifier, followed by a object. This method can only parse strings that follow the long date pattern in the `en-US` culture. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parseexact" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parseexact" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parseexact" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parseexact" language="vb"::: Each overload of the and methods also has an parameter that provides culture-specific information about the formatting of the string. The object is a object that represents a standard culture or a object that's returned by the property. also uses an additional string or string array argument that defines one or more custom date and time formats. @@ -118,15 +118,15 @@ The structure represents only a date without time informa The method converts common date string representations to a object. The method accepts various formats and uses the current culture or a specified culture for parsing. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parse" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parse" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parse" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parse" language="vb"::: ### DateOnly.ParseExact The method provides precise control over the expected format of the input string. Use this method when you know the exact format of the date string and want to ensure strict parsing. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parseexact" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parseexact" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parseexact" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parseexact" language="vb"::: The `ParseExact` method accepts either a single format string or an array of format strings, allowing you to parse dates that might come in multiple acceptable formats. @@ -144,15 +144,15 @@ The structure represents a time-of-day value, such as a d The method converts common time string representations to a object. The method accepts various formats including 12-hour and 24-hour notation. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parse" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parse" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parse" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parse" language="vb"::: ### TimeOnly.ParseExact The method provides precise control over the expected format of the input time string. Use this method when you know the exact format and want to ensure strict parsing. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parseexact" lang="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parseexact" lang="vb"::: +:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parseexact" language="csharp"::: +:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parseexact" language="vb"::: ## See also From 89caa3401ea8f13fe293fc5cb78f5335c726ed20 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 16:20:17 -0800 Subject: [PATCH 09/13] Add tryparse mentions --- docs/standard/base-types/parsing-datetime.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index 9c651ceab5221..157334fee3c22 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -32,8 +32,8 @@ Each type provides parsing methods that convert strings to their respective obje All three date and time types share similar parsing approaches: -- **Parse methods** - Convert many common string representations using current culture or specified culture settings. -- **ParseExact methods** - Convert strings that conform to specific format patterns, providing precise control over expected formats, including culture settings. +- **`Parse` and `TryParse` methods** - Convert many common string representations using current culture or specified culture settings. +- **`ParseExact` and `TryParseExact` methods** - Convert strings that conform to specific format patterns, providing precise control over expected formats, including culture settings. - **Format strings** - Define patterns for parsing using standard or custom format specifiers. Different cultures use different orders for day, month, and year. Some time representations use a 24-hour clock, others specify "AM" and "PM." The parsing methods handle these variations through culture-specific formatting rules. From 5350489c74f5ef111244c55c6a25fc49245873c3 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 16:21:17 -0800 Subject: [PATCH 10/13] actually fix snippets --- docs/standard/base-types/parsing-datetime.md | 32 ++++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index 157334fee3c22..803a66ea684e5 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -78,20 +78,20 @@ The format provider is also used to interpret an ambiguous numeric date. It's un The following example shows the use of the method to convert a `string` into a . This example uses the culture associated with the current thread. If the associated with the current culture can't parse the input string, a is thrown. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse" language="vb"::: You can also explicitly define the culture whose formatting conventions are used when you parse a string. You specify one of the standard objects returned by the property. The following example uses a format provider to parse a German string into a . It creates a representing the `de-DE` culture. That `CultureInfo` object ensures successful parsing of this particular string. This process precludes whatever setting is in the of the . -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" language="vb"::: However, you can use overloads of the method to specify custom format providers. The method doesn't support parsing non-standard formats. To parse a date and time expressed in a non-standard format, use the method instead. The following example uses the enumeration to specify that the current date and time information shouldn't be added to the for unspecified fields. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-nodefault" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-nodefault" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-nodefault" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-nodefault" language="vb"::: ### DateTime.ParseExact @@ -99,8 +99,8 @@ The method con In the following example, the method is passed a string object to parse, followed by a format specifier, followed by a object. This method can only parse strings that follow the long date pattern in the `en-US` culture. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parseexact" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parseexact" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parseexact" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parseexact" language="vb"::: Each overload of the and methods also has an parameter that provides culture-specific information about the formatting of the string. The object is a object that represents a standard culture or a object that's returned by the property. also uses an additional string or string array argument that defines one or more custom date and time formats. @@ -118,15 +118,15 @@ The structure represents only a date without time informa The method converts common date string representations to a object. The method accepts various formats and uses the current culture or a specified culture for parsing. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parse" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parse" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parse" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parse" language="vb"::: ### DateOnly.ParseExact The method provides precise control over the expected format of the input string. Use this method when you know the exact format of the date string and want to ensure strict parsing. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parseexact" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parseexact" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="dateonly-parseexact" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="dateonly-parseexact" language="vb"::: The `ParseExact` method accepts either a single format string or an array of format strings, allowing you to parse dates that might come in multiple acceptable formats. @@ -144,15 +144,15 @@ The structure represents a time-of-day value, such as a d The method converts common time string representations to a object. The method accepts various formats including 12-hour and 24-hour notation. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parse" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parse" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parse" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parse" language="vb"::: ### TimeOnly.ParseExact The method provides precise control over the expected format of the input time string. Use this method when you know the exact format and want to ensure strict parsing. -:::snippet source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parseexact" language="csharp"::: -:::snippet source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parseexact" language="vb"::: +:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="timeonly-parseexact" language="csharp"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="timeonly-parseexact" language="vb"::: ## See also From 6f6dbdf7575c93c619812e3b2293976c6cd9e466 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 16:45:05 -0800 Subject: [PATCH 11/13] Test swapping snippet syntax --- docs/standard/base-types/parsing-datetime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index 803a66ea684e5..a7d1d3169940f 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -83,7 +83,7 @@ The following example shows the use of the objects returned by the property. The following example uses a format provider to parse a German string into a . It creates a representing the `de-DE` culture. That `CultureInfo` object ensures successful parsing of this particular string. This process precludes whatever setting is in the of the . -:::code source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture" language="csharp"::: +:::code language="csharp" source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture"::: :::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" language="vb"::: However, you can use overloads of the method to specify custom format providers. The method doesn't support parsing non-standard formats. To parse a date and time expressed in a non-standard format, use the method instead. From 60736392c9cbd8d4003c589e9a036694e7085cc5 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 16:58:55 -0800 Subject: [PATCH 12/13] Try another way --- docs/standard/base-types/parsing-datetime.md | 2 +- .../base-types/snippets/parsing-datetime/csharp/Program.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index a7d1d3169940f..441aaa4f8beab 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -83,7 +83,7 @@ The following example shows the use of the objects returned by the property. The following example uses a format provider to parse a German string into a . It creates a representing the `de-DE` culture. That `CultureInfo` object ensures successful parsing of this particular string. This process precludes whatever setting is in the of the . -:::code language="csharp" source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime-parse-culture"::: +:::code language="csharp" source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime_parse_culture"::: :::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" language="vb"::: However, you can use overloads of the method to specify custom format providers. The method doesn't support parsing non-standard formats. To parse a date and time expressed in a non-standard format, use the method instead. diff --git a/docs/standard/base-types/snippets/parsing-datetime/csharp/Program.cs b/docs/standard/base-types/snippets/parsing-datetime/csharp/Program.cs index d7aa9083ce08c..85fc21653a2bf 100644 --- a/docs/standard/base-types/snippets/parsing-datetime/csharp/Program.cs +++ b/docs/standard/base-types/snippets/parsing-datetime/csharp/Program.cs @@ -53,7 +53,7 @@ static void DateTimeParseExactExample() } // - // + // static void DateTimeParseGermanExample() { var cultureInfo = new CultureInfo("de-DE"); @@ -63,7 +63,7 @@ static void DateTimeParseGermanExample() // The example displays the following output: // 6/12/2008 00:00:00 } - // + // // static void DateTimeParseNoDefaultExample() From a7b2bf21be80797595ef0cdf9feee2794a6eaa94 Mon Sep 17 00:00:00 2001 From: "Andy De George (from Dev Box)" Date: Fri, 16 Jan 2026 17:06:11 -0800 Subject: [PATCH 13/13] Another test --- docs/standard/base-types/parsing-datetime.md | 2 +- .../base-types/snippets/parsing-datetime/vb/Program.vb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/standard/base-types/parsing-datetime.md b/docs/standard/base-types/parsing-datetime.md index 441aaa4f8beab..f9d1f915415cc 100644 --- a/docs/standard/base-types/parsing-datetime.md +++ b/docs/standard/base-types/parsing-datetime.md @@ -84,7 +84,7 @@ The following example shows the use of the objects returned by the property. The following example uses a format provider to parse a German string into a . It creates a representing the `de-DE` culture. That `CultureInfo` object ensures successful parsing of this particular string. This process precludes whatever setting is in the of the . :::code language="csharp" source="./snippets/parsing-datetime/csharp/Program.cs" id="datetime_parse_culture"::: -:::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime-parse-culture" language="vb"::: +:::code source="./snippets/parsing-datetime/vb/Program.vb" id="datetime_parse_culture" language="vb"::: However, you can use overloads of the method to specify custom format providers. The method doesn't support parsing non-standard formats. To parse a date and time expressed in a non-standard format, use the method instead. diff --git a/docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb b/docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb index b01faa2656196..7cf1fd9c3557b 100644 --- a/docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb +++ b/docs/standard/base-types/snippets/parsing-datetime/vb/Program.vb @@ -46,7 +46,7 @@ Module Program End Sub ' - ' + ' Sub DateTimeParseGermanExample() Dim MyCultureInfo As New CultureInfo("de-DE") Dim MyString As String = "12 Juni 2008" @@ -55,7 +55,7 @@ Module Program ' The example displays the following output: ' 6/12/2008 00:00:00 End Sub - ' + ' ' Sub DateTimeParseNoDefaultExample()