Powershell convert date in variable to different date format - powershell-4.0

My script pulls the software installation date and puts it into $obj.InstallDate.
The value returned is in the format of 20131227. I want to convert this to December 27, 2013 but can't get this to work.
The Get-Date in powershell doesn't work for me. What do I need to do?

Try something like this
$DateValue = 20131228
Get-Date -Year $DateValue.ToString().Substring(0,4) -Month $DateValue.ToString().Substring(4,2) -Day $DateValue.ToString().Substring(6,2) -Format "MMMM dd, yyyy"

Related

How do I get this Get-Date format in powershell?

So, I'm trying to calculate the retirement date using Get-Date in Powershell. I only want to see the date, no time but also with the day of the week. I've been trying to use methods, no formats. But even with using formats it does not seem to work.
(Get-Date -Day 12 -Month 12 -Year 2000).addYears(67).ToLongDateString()
December 12, 2067
I'd like to see something like Tuesday, December 12, 2067. This is the last part of my script, I've been trying to do this for a while but can't seem to figure it out.
Check out custom date and time format strings.
(Get-Date -Day 12 -Month 12 -Year 2000).addYears(67).ToString("dddd, MMMM d, yyyy")
The result is as you wanted:
Monday, December 12, 2067

How to convert this date format

I have wierd date format:
ID, Time, Value 1 [mg/l]
9867, 43788.5946644, 5.266029
9851, 43788.5529745, 5.526279
9835, 43788.5113079, 6.008881
and I would like to convert it, but I can not even recognize this one. an anyone help me? It might be conversion to timestamp, simple date or anything else that is readable.
I know the outputs:
43788.5946644 - 19/11/2019 14:16
43788.5529745 - 19/11/2019 13:16
43788.5113079 - 19/11/2019 12:16
By accident I found an answer, this is a excell date. https://www.excel-exercise.com/date-format-in-excel/
can be converted using powershell command:
[DateTime]::FromOADate(43788.5946528) -> Tuesday, November 19 2019 14:16:18

How to get the current date and time in specific format in Shell

I just want to get the system current time using below format in shell
Expected Format:
2019-02-14T08:08:12.300Z
I tried below piece of script but It returned something different.
CURRENTDATE=`date +"%Y-%m-%d %T"`
echo $CURRENTDATE
Output
2019-02-27 01:22:57
I just ran date command in my linux box and got below response back:
-bash-3.2$ date
Wed Feb 27 01:43:26 PST 2019
We are passing the above output as input JSON file. But this output is not accepted by our JSON. So I just want to pass the date and time format in the above specified format (Expected Format).
You may use:
dt=$(date '+%Y-%m-%dT%T.%zZ')
echo "$dt"
2019-02-27T04:35:56.-0500Z
Also note use of --iso-8601=seconds:
date --iso-8601=seconds
2019-02-27T04:37:29-05:00
You can also use this one like
currentDate=$(date +"%Y-%m-%dT%H:%M:%SZ")
echo $currentDate
OutPut :-
2022-07-12T18:05:27Z

Windows ISO 8601 timestamp

I need to convert a date in Windows PowerShell to the ISO 8601 format.
In Linux/Unix it was no problem with
TZ=0 date -d "<random-format>" +%Y-%m-%dT%H:%M:%S.000Z
Now I need to do the same in Windows PowerShell. The output format on Windows is
Wednesday, 19. July 2017 01:06:13
How can I do it?
PowerShell's Get-Date supports standard .NET time formats. The o round-trip format complies with ISO 8601. Like so,
Get-Date -Format "o"
2017-08-15T12:10:34.4443084+03:00
Get-Date supports Unix formatting strings with the -UFormat parameter. You can reuse it:
Get-Date (Get-Date).ToUniversalTime() -UFormat '+%Y-%m-%dT%H:%M:%S.000Z'
The following works both in Windows PowerShell 5.1 and PowerShell 7.0 on Windows/OS X:
(Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffK")
2020-12-01T22:31:41.402Z
If you don't want your milliseconds, then format string would be "yyyy-MM-ddTHH:mm:ss.000K"
Old question but since none of the other answers has it, if you're looking for UTC, this seems to do it:
(Get-Date).ToUniversalTime().ToString("o")

Print the standard time - eg 04:52:06 PM - Bash

I have the
$ date +"%c"
command
which displays the date and time, however I only wish to display the time in full non military, eg: 04:52:06 PM
I have tried
date +"%T"
However that returns the Military Date, not standard.
Try making it from "scratch" using all the options together. You can have as many in the format as you like.
]$ date +"%I:%M:%S %p"
05:26:22 PM
Your system might accept date +%r that is an "alias" for "%I:%M:%S %p"
I would display it in this way:
date +"%I:%M:%S %p"

Resources