Skip to main content
ubuntuask.com

Back to all posts

How to Convert Localtime to Epoch Timestamp In Powershell?

Published on
4 min read
How to Convert Localtime to Epoch Timestamp In Powershell? image

Best Tools and Resources for PowerShell Scripting to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
4 The PowerShell Scripting & Toolmaking Book: Author-Authorized Second Edition

The PowerShell Scripting & Toolmaking Book: Author-Authorized Second Edition

BUY & SAVE
$29.99
The PowerShell Scripting & Toolmaking Book: Author-Authorized Second Edition
5 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$34.99
Learn PowerShell Scripting in a Month of Lunches
6 Trend EasyScribe Scribing Tool, Accurate Scribing Solution for Carpenters, Joiners, Tilers, Kitchen & Shop Fitters, E/SCRIBE, Black

Trend EasyScribe Scribing Tool, Accurate Scribing Solution for Carpenters, Joiners, Tilers, Kitchen & Shop Fitters, E/SCRIBE, Black

  • VERSATILE FOR ALL TASKS: PERFECT FOR DOORS, WINDOWS, FLOORING & MORE!

  • PRECISION ADJUSTMENTS: ENSURE PERFECT FIT WITH ADJUSTABLE OFFSET FEATURES.

  • ULTRA-THIN ACCESS: FITS INTO NARROW GAPS FOR OPTIMAL VERSATILITY IN INSTALLS.

BUY & SAVE
$31.99
Trend EasyScribe Scribing Tool, Accurate Scribing Solution for Carpenters, Joiners, Tilers, Kitchen & Shop Fitters, E/SCRIBE, Black
7 Milescraft 8407 ScribeTec - Scribing and Compass Tool

Milescraft 8407 ScribeTec - Scribing and Compass Tool

  • ARTICULATING HEAD FOR EASY MARKING AT COMPLEX ANGLES.
  • RETRACTABLE POINT ENSURES PRECISION FOR PERFECT CIRCLES.
  • INTEGRATED SHARPENER KEEPS YOUR PENCIL SHARP AND READY.
BUY & SAVE
$10.49 $10.99
Save 5%
Milescraft 8407 ScribeTec - Scribing and Compass Tool
8 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
9 FastCap ACCUSCRIBEPRO Accuscribe Scribing Tool

FastCap ACCUSCRIBEPRO Accuscribe Scribing Tool

  • ADJUSTABLE GRIP FOR STANDARD PENCILS ENSURES VERSATILE USE.
  • CONSISTENT SCRIBE OFFSET FOR PERFECT PARALLEL EVERY TIME.
  • DURABLE POLYMER DESIGN FOR LONG-LASTING PERFORMANCE.
BUY & SAVE
$17.99
FastCap ACCUSCRIBEPRO Accuscribe Scribing Tool
10 Mastering PowerShell Scripting for SysAdmins: Automating Complex Tasks with Confidence

Mastering PowerShell Scripting for SysAdmins: Automating Complex Tasks with Confidence

BUY & SAVE
$5.99
Mastering PowerShell Scripting for SysAdmins: Automating Complex Tasks with Confidence
+
ONE MORE?

To convert a local time to an epoch timestamp in PowerShell, you can use the following steps:

  1. Get the local time using the Get-Date cmdlet.
  2. Convert the local time to UTC time using the ToUniversalTime method.
  3. Calculate the number of seconds since the epoch (January 1, 1970) by subtracting the epoch time from the UTC time.
  4. This will give you the epoch timestamp in seconds.

Here is an example code snippet that demonstrates this conversion:

$localTime = Get-Date $utcTime = $localTime.ToUniversalTime() $epochTime = New-Object DateTime 1970,1,1,0,0,0,0 $epochTimestamp = ($utcTime - $epochTime).TotalSeconds Write-Output $epochTimestamp

You can run this code in a PowerShell script or directly in the PowerShell console to convert a local time to an epoch timestamp.

How to work with timezones in PowerShell?

Working with timezones in PowerShell can be done using the .NET framework classes included in PowerShell. Here are some examples of how to work with timezones in PowerShell:

  1. Get the list of available timezones:

[System.TimeZoneInfo]::GetSystemTimeZones()

  1. Convert a date/time to a different timezone:

$sourceTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Pacific Standard Time") $destinationTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

$sourceDateTime = Get-Date $destinationDateTime = [System.TimeZoneInfo]::ConvertTime($sourceDateTime, $sourceTimeZone, $destinationTimeZone)

  1. Get the current timezone of the system:

[System.TimeZoneInfo]::Local

  1. Convert a UTC date/time to a specific timezone:

$utcDateTime = Get-Date $destinationTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time") $localDateTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($utcDateTime, $destinationTimeZone)

These examples demonstrate how you can work with timezones in PowerShell using the .NET framework classes available. You can also perform other operations such as comparing timezones, calculating time differences between timezones, and formatting dates in specific timezones.

How to calculate the epoch timestamp for a specific date and time in PowerShell?

You can calculate the epoch timestamp for a specific date and time in PowerShell using the following code:

$dateTime = Get-Date "2023-09-15 12:00:00" $epochTime = [int][System.Math]::Round((Get-Date $dateTime).ToUniversalTime().Subtract((Get-Date "1970-01-01")).TotalSeconds)

Write-Output $epochTime

Replace the "2023-09-15 12:00:00" with the specific date and time you want to calculate the epoch timestamp for. This code will output the epoch timestamp for that date and time.

How to convert localtime to epoch timestamp in PowerShell?

You can convert a local date and time in PowerShell to an epoch timestamp by using the following commands:

  1. First, you need to get the current date and time:

$datetime = Get-Date

  1. Next, you can convert the local date and time to UTC:

$utcDateTime = $datetime.ToUniversalTime()

  1. Finally, you can convert the UTC date and time to an epoch timestamp:

$epochTimestamp = [int][double]::Parse(($utcDateTime - (Get-Date "1/1/1970")).TotalSeconds)

Now, the $epochTimestamp variable will contain the epoch timestamp for the local date and time.

How to retrieve the current date in PowerShell?

In PowerShell, you can retrieve the current date by using the Get-Date cmdlet. This cmdlet returns the current date and time in a datetime object format.

Here is an example of how to retrieve the current date in PowerShell:

$CurrentDate = Get-Date $CurrentDate

When you run this code in PowerShell, it will display the current date and time in the output.

You can also format the output to display only the date in a specific format. For example, to display the current date in the "yyyy-MM-dd" format, you can use the following code:

$CurrentDate = Get-Date -Format "yyyy-MM-dd" $CurrentDate

This will display the current date in the specified format in the output.

How to get the current system time in PowerShell?

To get the current system time in PowerShell, you can use the Get-Date cmdlet. Here's an example of how you can use it:

$currentTime = Get-Date Write-Output "Current system time is: $currentTime"

When you run this script in PowerShell, it will display the current system time in the output.