Converts a Coordinated Universal Time (UTC) to the time in a specified time zone.
public:
static DateTime ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo ^ destinationTimeZone);
public static DateTime ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo destinationTimeZone);
static member ConvertTimeFromUtc : DateTime * TimeZoneInfo -> DateTime
Public Shared Function ConvertTimeFromUtc (dateTime As DateTime, destinationTimeZone As TimeZoneInfo) As DateTime
Parameters
The Coordinated Universal Time (UTC).
The time zone to convert dateTime
to.
The date and time in the destination time zone. Its Kind property is Utc if destinationTimeZone
is Utc; otherwise, its Kind property is Unspecified.
destinationTimeZone
is null
.
The following example converts Coordinated Universal Time (UTC) to Central Time.
DateTime timeUtc = DateTime.UtcNow;
try
{
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
Console.WriteLine("The date and time are {0} {1}.",
cstTime,
cstZone.IsDaylightSavingTime(cstTime) ?
cstZone.DaylightName : cstZone.StandardName);
}
catch (TimeZoneNotFoundException)
{
Console.WriteLine("The registry does not define the Central Standard Time zone.");
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("Registry data on the Central Standard Time zone has been corrupted.");
}
let timeUtc = DateTime.UtcNow
try
let cstZone = TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time"
let cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone)
printfn $"The date and time are {cstTime} {if cstZone.IsDaylightSavingTime cstTime then cstZone.DaylightName else cstZone.StandardName}."
with
| :? TimeZoneNotFoundException ->
printfn "The registry does not define the Central Standard Time zone."
| :? InvalidTimeZoneException ->
printfn "Registry data on the Central Standard Time zone has been corrupted."
Dim timeUtc As Date = Date.UtcNow
Try
Dim cstZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")
Dim cstTime As Date = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone)
Console.WriteLine("The date and time are {0} {1}.", _
cstTime, _
IIf(cstZone.IsDaylightSavingTime(cstTime), _
cstZone.DaylightName, cstZone.StandardName))
Catch e As TimeZoneNotFoundException
Console.WriteLine("The registry does not define the Central Standard Time zone.")
Catch e As InvalidTimeZoneException
Console.WriteLine("Registry data on the Central Standard Time zone has been corrupted.")
End Try
Remarks
When performing the conversion, the ConvertTimeFromUtc method applies any adjustment rules in effect in the destinationTimeZone
time zone.
The precise behavior of this method depends on the value of the Kind property of the dateTime
parameter, as the following table shows.
If the conversion of dateTime
results in a date and time value that is earlier than DateTime.MinValue or later than DateTime.MaxValue, this method returns DateTime.MinValue or DateTime.MaxValue, respectively.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4