I need to format the previous date to yyyy-mm-dd.
For example, today is May 25,2016. Therefore I need the output '2016-05-24'.
$a = (Get-Date).AddDays(-1).ToString('yyyy-mm-dd')
Write-Output $a
When I run the code I get 2016-44-24 which is incorrect.
How can I do it?
mm specifies The minute, from 00 through 59
Source.
You have to use uppercase for month instead:
(Get-Date).AddDays(-1).ToString('yyyy-MM-dd')
Related
I want to change dynamically for below URL.
e.g let's say if I run the script in february, the url would be like below such as m-1-2023.
or if I run the script in march, the url would be like m-2-2023.
if I run the script in january 2023, the url would be like m-12-2022.
and so on.
As summary , it will be the previous month.
My URL:
https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-1-2023
Thanks,
Use Get-Date to get a DateTime object, which has support for date arithmetics and formatting. Get last month by adding -1 months and format with .Net format strings Like so,
$d = (get-date).AddMonths(-1).ToString("M-yyyy")
$url = "https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-" + $d
# output
https://app.contoso.com/api/v1/reports/billing/aws?filter%5Bdate_range%5D=m-1-2023
I try to get the Day as a two digit number out of Get-Date in PowerShell.
When I try (Get-Date).Day the result will be for example 6 but I want to have it as 06. Also for the month.
How can this be done?
I have already tried things like (Get-Date).Day.ToString("dd") but it doesn't work.
Using ToString() and supplying date formatters (e.g. "yyyy" or "dd") will only work on dates. By accessing .Day or .Year, the operation is instead attempted on an integer, which will fail.
Try (for the day):
(Get-Date).ToString("dd")
...and (and for the month):
(Get-Date).ToString("MM")
See here for custom formatting of dates using ToString()
What is the command for displaying the time and date in qbasic? Could the syntax for the commands be given as well? And an explanation if possible?
You can use DATE$ and TIME$
These can also set the date and time as well.
The command for printing the time(current system time) is time$
The time$ is actually a function, in this case, no parameter is needed.
And the code is...
PRINT TIME$
The time is printed in hh: mm: ss format(hour: minutes: seconds).
And therefore the output would be something like this:
14:55:28
For printing the current system date, we use date$ function which is also a string function
The code is:
PRINT DATE$
The date is printed in mm-dd-yyyy format or month-day-year(American date format).
Hence the output will be:
02-17-2018
Hope it helps...
The QB date/time functions are:
DATE$ returns the date in a string in the form MM-DD-YYYY
TIME$ returns the time in a string in the form HH:MM:SS
When used as a command the date$ and time$ can be assigned to set the system date and time, for example DATE$ = "12-10-1990" or TIME$ = "12:10:10"
If the year is a leap year then the 29th day of February could be set. Otherwise if it is not a leap year then a syntax error will occur trying to set the date in February to the 29th.
The actual line in the PowerShell script that is desired is:
$tsd = [datetime]::ParseExact($TSDiff,'yyyyMMddhhmmsstt',$null)
But the $TSDiff variable being used has time expressed as, without AM/PM:
20171023212800
This is a 24-hour format where 11 pm is represented by 23. It was retrieved using an FTP request which seems to only return 24 hour format strings without AM/PM.
Breaking this down, the following PowerShell command works:
[datetime]::ParseExact("20171023092800",'yyyyMMddhhmmss',$null)
But the following PowerShell command does not work:
[datetime]::ParseExact("20171023212800",'yyyyMMddhhmmss',$null)
The reason the second line doesn't work is clear; the hour digits are in 24-hour format, as in the $TSDiff listed at the beginning of this post.
Is there a simple way in PowerShell to convert the string 20171023212800 to 20171023092800PM?
From Formatting Dates and Times
[...]
h, %h - The hour in a 12-hour clock. Single-digit hours will not have a leading zero. Specify %h if the format pattern is not combined with other format patterns.
hh - The hour in a 12-hour clock. Single-digit hours will have a leading zero.
H, %H - The hour in a 24-hour clock. Single-digit hours will not have a leading zero. Specify %H if the format pattern is not combined with other format patterns.
HH - The hour in a 24-hour clock. Single-digit hours will have a leading zero.
[...]
While you are converting your datetime string to a 12-hour formatted string with hh in the format specifier, it will convert to a 24-hour string with HH in it like:
[datetime]::ParseExact("20171023212800",'yyyyMMddHHmmss',$null)
Use:
# Method 1. Use HH for 24-hour format like TessellatingHeckler proposes
[datetime]::ParseExact("20171023212800", 'yyyyMMddHHmmss', $null)
# Method 2. If you are not sure your string is
# date, use TryParse with the same format
[datetime]$dirDate = New-Object DateTime
if ([DateTime]::TryParseExact(
'20171023212800',
'yyyyMMddHHmmss',
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::None,
[ref]$dirDate))
{
$dirDate
}
This line dose a fine job of renaming my file but it only use the hours 00-12 and not 13-24. After 12 it begins with 01. Sins it does not append AM or PM you cant know bu just looking at the file name when it was created. I would like it to use 24h format.
dir C:\script\logged_in_users-.csv | Rename-Item -NewName {$_.BaseName+(Get-Date -f yyyy-MM-dd-hh)+$_.Extension}
In your format string, use HH instead of hh, to output hours in 24-hour format.
Swap hh for HH for 24-hour clock timestamp.
See: http://technet.microsoft.com/en-us/library/ee692801.aspx