0

I need your help

I have output from csv file which look like below, and I need to convert dates into Unix timestamp. Header "Scheduled Start Date Time" can be removed by simple replace command in order to keep only dates, however each row need to be changed into UNIX timestamp.

Scheduled Start Date Time
2020-04-14 19:00
2020-04-15 06:00
2020-04-15 06:00
2020-04-15 14:00
2020-04-15 14:00
2020-04-15 09:00
2020-04-15 14:00
2020-04-15 09:00

I tried something like that, but it is not working:

$trimfile = Get-Content -Path "C:\Users\timestamp.txt"
Get-Date -Date $trimfile -UFormat %s | Out-File "C:\Users\timestamp1.txt"
majan
  • 125
  • 12
  • 1
    What do you consider a "UNIX timestamp"? What is the actual format you're after? – Jeroen Mostert Apr 17 '20 at 11:37
  • take a look at this post >>> c# - How can I convert a DateTime to the number of seconds since 1970? - Stack Overflow — https://stackoverflow.com/questions/3354893/how-can-i-convert-a-datetime-to-the-number-of-seconds-since-1970 << – Lee_Dailey Apr 17 '20 at 12:05
  • Unix time stamp? The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch. It should also be pointed out (thanks to the comments from visitors to this site) that this point in time technically does not change no matter where you are located on the globe. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side. – majan Apr 20 '20 at 10:05
  • 1586890800 Is equivalent to: 04/14/2020 @ 7:00pm – majan Apr 20 '20 at 10:06

1 Answers1

1

Unix epoch starts at Jan 1 1970, so just subtract that date from your current date and count the number of seconds in between:

# Read all timestamps into variable
$timestamps = Get-Content C:\Users\timestamp.txt |Select -Skip 1

# Define unix epoch
$unixEpoch  = Get-Date '1970-01-01'

# Loop over all timestamps
for($i = 1; $i -le $timestamps.Count; $i++){
  # Calculate UTC corresponding to the timestamp
  $currentUtc = (Get-Date $timestamps[$i-1]).ToUniversalTime()

  # Calculate + round total number of seconds since epoch
  $totalSeconds = ($currentUtc - $unixEpoch).TotalSeconds
  [Math]::Round($totalSeconds) |Out-File "C:\Users\timestamp${i}.txt"
}

If the locale on the machine doesn't recognize the timestamp format, use [datetime]::Parse($timestamp[$i-1], [cultureinfo]::InvariantCulture) instead of Get-Date $timestamp[$i-1]

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Hi, looks like it is works, however I am getting manyfiles with one values, and I would like to redirect everything into one file. – majan Apr 20 '20 at 10:32
  • Additionall time is in Univesral Time and I need in CEST time (+2h) – majan Apr 20 '20 at 10:54
  • @majan then remove the `${i}` in the `Out-File` file name. And unix timestamps are timezone independent, if you run this on a system with system time zone +2 it works – Mathias R. Jessen Apr 20 '20 at 11:11
  • If I remove ${i} in the Out-File then in file I have only 1 entry from first row. – majan Apr 20 '20 at 11:29
  • @majan add `-Append` to `Out-File` – Mathias R. Jessen Apr 20 '20 at 11:39
  • Hi, I moved script to another server and now I am getting such error:`Get-Date : Cannot bind parameter 'Date'. Cannot convert value "29/01/2020 00:00:00" to type "System.DateTime". Error: "String was not recognized as a valid DateTime.` any ideas? – majan May 06 '20 at 10:46
  • `PS> Get-Date Wednesday, May 6, 2020 12:49:17 PM` – majan May 06 '20 at 10:49
  • @majan Added a note at the bottom of the answer on how to force `DateTime.Parse()` to use english formatting – Mathias R. Jessen May 06 '20 at 14:52