Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get 'date-1' formatted as mm-dd-yyyy using PowerShell?

How does one get date-1 and format it to mm-dd-yyyy in PowerShell?

Example: If today is November 1, 2013, and I need 10-31-2013 in my code.

I've used AddDays(-1) before, but I can't seem to get it to work with any formatting options.

like image 635
pburgh Avatar asked Nov 02 '13 12:11

pburgh


People also ask

How do I manipulate a date in PowerShell?

Using the Get-Date Command The number one command to consider when working with dates and times in PowerShell is the Get-Date command. By running Get-Date just by itself, you will output a DateTime object with the current date and time. Using the Get-Member command, we can see that the output is a System.

How do you format a date in mm dd yyyy?

dd/MM/yyyy — Example: 23/06/2013. yyyy/M/d — Example: 2013/6/23. yyyy-MM-dd — Example: 2013-06-23. yyyyMMddTHH:mmzzz — Example: 20130623T13:22-0500.


1 Answers

You can use the .tostring() method with datetime format specifiers to format to whatever you need:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

(Get-Date).AddDays(-1).ToString('MM-dd-yyyy') 11-01-2013 
like image 163
mjolinor Avatar answered Sep 22 '22 06:09

mjolinor