Date format not applied when doing AddDay - powershell

I've got the following two queries:
$startDate = (Get-Date -format s).AddDays(-6)
$endDate = (Get-Date -format s)
Write-Host $startDate
Write-Host $endDate
//Prints
2015-05-02 16:23:52
2015-05-08T16:47:56
I'd like to understand how to put the T back in the first printed dates. The space breaks my powershell script where I add that date to an LogParser.sql path:
Myquery.Sql?startdate='$startdate'+enddate='$enddate')

This line:
$startDate = (Get-Date -format s).AddDays(-6)
throws an error (about [system.string] not having a function AddDays) for me with powershell 3.0.
I can get the output you want from this though:
"{0:s}" -f (Get-Date).AddDays(6)

Related

Error with formatting a date when using addhours()

I'm using Powershell v2 to add hours to a date, but I always get an error.
This code below work fine:
$date = Get-Date -format "yyyyMMddhhmmss"
$DateStr ='{0:yyyyMMddhhmmss}' -f $Date
Write-Host $date
But when I use addhours():
$oriDate=(Get-Date).addhours(1)
$date = $oriDate -format "yyyyMMddhhmmss"
$DateStr ='{0:yyyyMMddhhmmss}' -f $Date
Write-Host $date
I get the error:
You must provide a value expression on the right-hand side of the '-f' operator.
There is a simpler way to get what you're after. Check out the Get-Date reference page for examples and generally useful info.
(Get-Date).ToString("yyyyMMddhhmmss")
$((Get-Date).AddHours(1)).ToString("yyyyMMddhhmmss")

Date time parsing to another format

I need to get the end date of the month we are in.
I tried with this code:
$CURRENTDATE=GET-DATE -Format "dd/MM/yyyy"
$FIRSTDAYOFMONTH=GET-DATE $CURRENTDATE -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)
echo $LASTDAYOFMONTH
This gives me the date of the end of the month. But i need it in another format (dd/MM/yyyy).
But can't seem to get my head around it. A push in the right direction would be great.
Adapting Mike F. Robbins code
$currentDate = Get-Date
$lastDay = [DateTime]::DaysInMonth($currentDate.Year, $currentDate.Month)
$lastDayOfMonth = Get-Date ([DateTime]"$($currentDate.Month), $LastDay, $($currentDate.Year)") -Format 'dd/MM/yyyy'
Write-Host $lastDayOfMonth
Or if you want to use your existing code:
$CurrentDate = Get-Date -Format "dd/MM/yyyy"
$FirstDayOfMonth = Get-Date $CurrentDate -Day 1
$LastDayOfMonth = Get-Date $FirstDayOfMonth.AddMonths(1).AddSeconds(-1) -Format "dd/MM/yyyy"
Write-Output $LastDayOfMonth

Check if date in past

$olddate = Get-Date -Date "31-dec-2013 00:00:00" -Format d.M.yyyy
$Now = Get-date -Format d.M.yyyy
How can I check if $olddate is earlier than $Now?
If your store them as DateTime instead of as formatted strings, you can use the less-than operator (-lt) just like with regular numbers, and then use the format operator (-f) when you need to actually display the date:
$olddate = Get-Date -Date "31-dec-2013 00:00:00"
$Now = Get-Date
if($olddate -lt $Now){
"`$olddate is in the past!"
"{0:d.M.yyyy}" -f $olddate
"{0:d.M.yyyy}" -f $Now
}
You can also do a comparison on Ticks. I.e.: $olddate.Ticks will be -lt then $Now.Ticks.

Finding current quarter of year in PowerShell to append in a filename with format "yyyyqq"

I am trying to find current quarter of year in PowerShell to append in a filename with format yyyyqq.
I have already got current month and week as below:
$WeekYYYYWW = (get-date (Get-Date).AddDays(-7) -UFormat "%Y%V")
$yesterdayMMDDYY=(get-date (get-date).AddDays(-1) -uformat %m%d%Y)
$monthly=(get-date (get-date).AddDays(-1) -uformat %m%Y)
Another way is:
"$(Get-date -f yyyy)$("{0:00}" -f [Math]::ceiling((Get-date -f MM)/3) )"
one way is:
$yearYYYYQQ = "$(get-date -uformat %Y)$("{0:00}" -f [int]((get-date).month/4) )"
Another way:
$today = Get-Date
$YearYYYYQQ = "{0}{1:d2}" -f ($today.Year, [int][math]::Ceiling($today.Month/3))
"$([datetime]::Now.Year)$(([math]::Ceiling(([datetime]::Now.Month)/3)).ToString().PadLeft(2,"0"))"

powershell : how to format get-date to string and remove 0's?

I'm attempting to eliminate leading any leading zeroes in my date when I run the get-date cmdlet by trying:
$filedate = get-date -uformat "%m-%d-%Y"
$filedate = $filedate.ToString().Replace("0", "")
this returns "01-04-2008"
I want to the output to be "1-4-2008"
any ideas on another way of doing this?
thanks in advance
$filedate = get-date -format "M-d-yyyy"
$fileDate = $fileDate.ToString("M-d-yyyy")
use format string like :
get-date -format yyyy/M/d
or
get-date.tostring(yyyy/M/d)