How to Convert Utc Datetime String In Powershell?

8 minutes read

In PowerShell, you can convert a UTC datetime string using the [datetime]::ParseExact method. This method allows you to specify the format of the datetime string so that PowerShell can accurately convert it to a datetime object. The format string for a UTC datetime string typically looks like "yyyy-MM-ddThh:mm:ssZ".


Here's an example of how you can convert a UTC datetime string in PowerShell:

1
2
$utcDateString = "2021-10-15T14:30:00Z"
$utcDateTime = [datetime]::ParseExact($utcDateString, "yyyy-MM-ddTHH:mm:ssZ", [System.Globalization.CultureInfo]::InvariantCulture)


After running this code, the variable $utcDateTime will contain a datetime object that represents the UTC datetime specified in the $utcDateString variable. You can then use this datetime object for further processing or conversion as needed.

Best PowerShell Books to Read in December 2024

1
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Rating is 5 out of 5

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

2
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

Rating is 4.9 out of 5

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

3
Scripting: Automation with Bash, PowerShell, and Python

Rating is 4.8 out of 5

Scripting: Automation with Bash, PowerShell, and Python

4
Learn PowerShell Scripting in a Month of Lunches

Rating is 4.7 out of 5

Learn PowerShell Scripting in a Month of Lunches

5
Mastering PowerShell Scripting - Fourth Edition: Automate and manage your environment using PowerShell 7.1

Rating is 4.6 out of 5

Mastering PowerShell Scripting - Fourth Edition: Automate and manage your environment using PowerShell 7.1

6
Practical Automation with PowerShell: Effective scripting from the console to the cloud

Rating is 4.5 out of 5

Practical Automation with PowerShell: Effective scripting from the console to the cloud

7
Mastering PowerShell Scripting - Fifth Edition: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Rating is 4.4 out of 5

Mastering PowerShell Scripting - Fifth Edition: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

8
PowerShell for Sysadmins: Workflow Automation Made Easy

Rating is 4.3 out of 5

PowerShell for Sysadmins: Workflow Automation Made Easy

  • Book - powershell for sysadmins: workflow automation made easy
9
PowerShell Pocket Reference: Portable Help for PowerShell Scripters

Rating is 4.2 out of 5

PowerShell Pocket Reference: Portable Help for PowerShell Scripters


How to convert utc datetime string in powershell using custom functions?

To convert a UTC datetime string in Powershell using custom functions, you can create a function that takes the UTC datetime string as input and converts it to the local timezone. Here's an example of how you can achieve this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
function ConvertFromUtcToLocal {
    param (
        [string]$utcDateTime
    )

    $utcTime = [datetime]::ParseExact($utcDateTime, "yyyy-MM-ddTHH:mm:ssZ", [System.Globalization.CultureInfo]::InvariantCulture)
    $localTime = $utcTime.ToLocalTime()

    return $localTime.ToString("yyyy-MM-dd HH:mm:ss")
}

# Test the function
$utcDateTime = "2022-01-01T12:00:00Z"
$localDateTime = ConvertFromUtcToLocal $utcDateTime
Write-Output "UTC datetime: $utcDateTime"
Write-Output "Local datetime: $localDateTime"


In this example, the ConvertFromUtcToLocal function takes a UTC datetime string in the format "yyyy-MM-ddTHH:mm:ssZ" and converts it to the local timezone. The function uses the ParseExact method to parse the UTC datetime string and then calls the ToLocalTime method to convert it to the local timezone. Finally, the function returns the local datetime string in the format "yyyy-MM-dd HH:mm:ss".


You can test the function by passing a UTC datetime string to it and then printing the original UTC datetime string and the converted local datetime string.


How to convert utc datetime string in powershell to milliseconds?

You can convert a UTC datetime string to milliseconds in PowerShell by using the following code:

1
2
3
$utcDateString = "2021-06-13T15:30:00Z"
$utcDateTime = [datetime]::ParseExact($utcDateString, "yyyy-MM-ddTHH:mm:ssZ", [System.Globalization.CultureInfo]::InvariantCulture)
$utcMilliseconds = ([int64]$utcDateTime.ToUniversalTime().Subtract([datetime]'1970-01-01 00:00:00').TotalMilliseconds)


This code first parses the UTC datetime string to a datetime object using ParseExact method and then converts it to milliseconds by subtracting it from the Unix Epoch time (1970-01-01 00:00:00) and getting the total milliseconds.


How to convert utc datetime string in powershell to hours?

You can convert a UTC datetime string to hours in PowerShell by using the following code:

1
2
3
4
$utcDateTimeString = "2022-01-02T15:30:00Z"
$utcDateTime = [datetime]::ParseExact($utcDateTimeString, "yyyy-MM-ddTHH:mm:ssZ", $null)
$hours = $utcDateTime.ToLocalTime().Hour
Write-Output $hours


In this code snippet:

  1. Replace the $utcDateTimeString with your UTC datetime string.
  2. Parse the UTC datetime string using [datetime]::ParseExact() method with the format "yyyy-MM-ddTHH:mm:ssZ".
  3. Use the ToLocalTime() method to convert the UTC datetime to local time.
  4. Get the hour component of the converted datetime using the Hour property.
  5. Output the hours value using Write-Output.


This will give you the hour component of the converted datetime in local time.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To convert from local timezone to UTC in Rust, you can use the chrono crate. First, you need to create a DateTime object representing the time in your local timezone. Then, you can use the .with_timezone() method to convert it to UTC. Here is an example code s...
To convert a time string in UTC to CST in Oracle, you can use the function TO_TIMESTAMP_TZ to convert the time string to a timestamp with time zone in UTC. Then, you can use the function FROM_TZ to convert the timestamp to the desired time zone, in this case C...
To find the closest midnight to a datetime in Pandas, you can use the following steps:Convert the datetime column to a Pandas datetime data type if it's not already in that format. You can use the pd.to_datetime() function for this. Use the floor() functio...