Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup default date format like yyyy-mm-dd in Powershell?

A simple & short question:

How can I setup a default date format in powershell like yyyy-mm-dd ? so any date output will be like this format?

or How to setup a date format globally in one script ?

Is there a way to output date only without time? when I output LastWriteTime, Default is

13-03-2014 14:51

I only need 13-03-2014 but 14:51.

like image 436
Root Loop Avatar asked Apr 03 '14 01:04

Root Loop


People also ask

How do I change the date format in PowerShell?

One way to change the date format of a DateTime object is by using Get-Date to generate the object and the Format parameter to change the format.

What is this DateTime format?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

How do you split in PowerShell?

The Split operator in PowerShell uses a regular expression in the delimiter, rather than a simple character. Maximum number of substrings. The default is to return all substrings. If you specify a number less than the number of substrings, the remaining substrings are concatenated in the last substring.


3 Answers

A date in PowerShell is a DateTime object. If you want a date string in a particular format, you can use the built-in string formatting.

PS C:\> $date = Get-Date
PS C:\> $date.ToString("yyyy-MM-dd")
2014-04-02

You can also use the string format (-f) operator:

PS C:\> "{0:yyyy-MM-dd}" -f $date
2014-04-02

The LastWriteTime property of a file is a DateTime object also, and you can use string formatting to output a string representation of the date any way you want.

You want to do this:

Get-ChildItem -Recurse \\path\ -filter *.pdf | Select-Object LastWriteTime,Directory

You can use a calculated property:

Get-ChildItem C:\Users\Administrator\Documents -filter *.pdf -Recurse |
  Select-Object Directory, Name, @{Name="LastWriteTime";
  Expression={$_.LastWriteTime.ToString("yyyy-MM-dd HH:mm")}}

Run

help Select-Object -Full

and read about calculated properties for more information.

like image 91
Bill_Stewart Avatar answered Oct 11 '22 22:10

Bill_Stewart


for always usage you can add in your .\Documents\WindowsPowerShell\profile.ps1

$culture = (Get-Culture).Clone()
$culture.DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
Set-Culture $culture
like image 33
Alban Avatar answered Oct 11 '22 22:10

Alban


i've used this, it works for me, just copy it at the beginning of your script

$currentThread = [System.Threading.Thread]::CurrentThread
$culture = [CultureInfo]::InvariantCulture.Clone()
$culture.DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
$currentThread.CurrentCulture = $culture
$currentThread.CurrentUICulture = $culture

in case you'll find problem in loading assembly for CultureInfo (i had this issue on Windows 2008 Server), change line 2 in this way

$currentThread = [System.Threading.Thread]::CurrentThread
$culture = $CurrentThread.CurrentCulture.Clone()
$culture.DateTimeFormat.ShortDatePattern = 'dd-MM-yyyy'
$currentThread.CurrentCulture = $culture
$currentThread.CurrentUICulture = $culture
like image 23
Mosè Bottacini Avatar answered Oct 11 '22 22:10

Mosè Bottacini