0

I am quite new in coding. Here's is the point I have an issue in PowerShell.

I get a variable from a csv which I called $datefinder = (Get-Date -Format "yyyy/MM/dd HH:mm")

I get the right answer for this variable in my loop, but the in the same loop I need my variable $datefinder with 30 more days.

I've tried to create a new variable like:

$2ndeDate = $datefinder.adddays(+30)

My variable datefinder is a string so I need to pass it into date. I've tried many things.

Here is the first line of the csv file

UCB63_DATENUM;U6618_FILENAME;UF6E8_CANAL;U65B8_IDRP
7/8/19 22:27;457E6659_ZN_LIQRLVPR_A_V_ML.pdf;ML;1367091
1
  • 1
    The format of the date in your example is not what you described.. In the CSV it is formatted M/d/yy HH:mm or d/M/yy HH:mm. Please find a better example csv line where it is clear if the Month or the day comes first.
    – Theo
    Commented Jul 2, 2020 at 18:00

1 Answer 1

0

It is always a good idea to use date variables as real DateTime objects. Only when you need these formatted for some reason, you turn them into strings in a certain format.

Luckily the reverse is also possible.
According to the example you just gave, you need to parse the date from the CSV either like this:

$datefinder = [datetime]::ParseExact('7/8/19 19:18', 'M/d/yy HH:mm', $null)
# gets you the date Monday, July 8 2019 19:18:00

or like this:

$datefinder = [datetime]::ParseExact('7/8/19 19:18', 'd/M/yy HH:mm', $null)
# gets you the date Wednesday, August 7 2019 19:18:00

Having this date as DateTime object, you can use its method AddDays() to add a number of days:

$2ndDate = $datefinder.AddDays(30)  # 30 days in the future as of the date in $datefinder

or

$2ndDate = (Get-Date).AddDays(-30)  # 30 days in the past

Hope that helps somewhat

10
  • the thing is i need to update the date finder (+30) this solution $2ndDate = $datefinder.AddDays(30) didn't work because it is see as a string Commented Jul 2, 2020 at 17:53
  • I got the date from the variable ; $datefinder = (Get-Date -Format "yyyy/MM/dd HH:mm") and the result is a string. As you can see the date comes from the csv, (sorry badly fromated) in this case 7/8/19 22:27. Commented Jul 2, 2020 at 17:55
  • @HadrienBeaujean Yes, because in that line you explicitely convert it to a string with the -Format operator. If you want it to be a DateTime object, just do $datefinder = Get-Date
    – Theo
    Commented Jul 2, 2020 at 17:58
  • I've also tried the parse method but maybe i made a mistake in the syntax Commented Jul 2, 2020 at 18:05
  • @HadrienBeaujean I have edited the answer to get a datetime object from the (string) date in the CSV. Because the example isn't clear enough, the format could be 'M/d/yy HH:mm' or 'd/M/yy HH:mm'. I've put in parsing methods for both variations.
    – Theo
    Commented Jul 2, 2020 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.