Niag Ntawv
Niag Ntawv

Reputation: 337

Retrieve datetime object in Powershell without the time portion

Is it possible to retrieve only the date portion of a datetime object in PowerShell? Reason being I need to compare the LastWriteTime property of files with today's date to determine whether to backup a file or not. As-is a datetime object includes the time as well which will always evaluate to false when I do something like:

if ($fileDate -eq $currentDate) {
    # Do backup
}

I haven't found anyway to do this. If we use the format operator or a method, it converts the object to a string object. If you try to convert that back to a datetime object, it appends the time back onto the object. Probably something simple, but I've been looking at this script for a while and that's the last part that's breaking.

Upvotes: 7

Views: 20985

Answers (4)

Johan Myréen
Johan Myréen

Reputation: 226

Assuming $currentTime contains a DateTime object, you can retrieve a new DateTime object with the same date but with the time portion zeroed like this:

$midnight = Get-Date $currentTime -Hour 0 -Minute 0 -Second 0 -Millisecond 0

Upvotes: 5

Simon Elms
Simon Elms

Reputation: 19808

EDIT: As @jessehouwing points out in the comments below, my answers are unnecessarily complicated. Just use $datetime.Date.


A couple of ways to get a DateTime without any time component (ie set to midnight at the start of the date in question, 00:00:00):

$dateTime = <some DateTime>
$dateWithoutTime = $dateTime.AddSeconds(-$dateTime.Second).AddMinutes(-$dateTime.Minute).AddHours(-$dateTime.Hour)

or

$dateTime = <some DateTime>
$dateWithoutTime = Get-Date -Date ($dateTime.ToString("yyyy-MM-dd"))

I ran each version in a loop, iterating 100,000 times. The first version took 16.4 seconds, the second version took 26.5 seconds. So I would go with the first version, although it looks a little more complicated.

Based on answers found here: https://techibee.com/powershell/powershell-how-to-query-date-time-without-seconds/2737 (that article is about stripping just the seconds from a DateTime. But it can be extended to stripping hours, minutes and seconds).

Upvotes: 6

Raimonds Virtoss
Raimonds Virtoss

Reputation: 11

Assuming $fileDate is not a dateTime object, then you can just convert both to strings and format them.

if ($fileDate.ToString() -eq $currentDate.ToString("dd/MM/yyyy")) {
    # Do backup
}

Upvotes: 1

M Antonio
M Antonio

Reputation: 144

This will not answer how to remove time on datetime, but to do your validation purpose of identifying when to backup.

I do suggest to subtract your two given date values and compare the result if total hours are already met to do your backup.

if (( $currentDate - $fileDate ).TotalDays > 7) {
# Do Backup
}

you can also validate for the following

Days              : 
Hours             : 
Minutes           : 
Seconds           : 
Milliseconds      : 
Ticks             :
TotalDays         : 
TotalHours        : 
TotalMinutes      : 
TotalSeconds      : 
TotalMilliseconds : 

Upvotes: 1

Related Questions