none
Problem executing function RRS feed

  • Question

  • This is a function call to get week number of a file. Eg, if a file is created on first week of January, then the function is supposed to return 1. This is the script (ReturnValues.ps1) that calls the function :

    function Get-WeekOfMonth([String]$Date) {
        $dateTime = Get-Date $Date
        $firstDayOfMonth = Get-Date $Date -Day 1
    
        $offsetDay = $dateTime.Day + $firstDayOfMonth.DayOfWeek
    
        [Math]::Ceiling($offsetDay / 7)
    }

    To call the function, from the Powershell prompt, I execute this :

    PS C:\Users\Owner\Desktop> . .\ReturnValues.ps1
    PS C:\Users\Owner\Desktop> Get-WeekOfMonth

    I get this error afterwards :

    PS C:\Users\Owner\Desktop> Get-WeekOfMonth
    Get-Date : Cannot bind parameter 'Date'. Cannot convert value "" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
    At C:\Users\Owner\Desktop\ReturnValues.ps1:2 char:26
    +     $dateTime = Get-Date $Date
    +                          ~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
     
    Get-Date : Cannot bind parameter 'Date'. Cannot convert value "" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
    At C:\Users\Owner\Desktop\ReturnValues.ps1:3 char:33
    +     $firstDayOfMonth = Get-Date $Date -Day 1
    +                                 ~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
     
    0
    
    PS C:\Users\Owner\Desktop> 

    How do I resolve this?

    Tuesday, January 21, 2020 6:11 AM

All replies

  • You can't use a null date in your function.

    \_(ツ)_/

    Tuesday, January 21, 2020 6:25 AM
  • This is your second question that is based on guesswork.  You need to take time to learn basic PowerShell,.  You cannot learn PowerShell by guessing.

    Learning to script properly with PowerShell


    \_(ツ)_/

    Tuesday, January 21, 2020 6:27 AM
  • This will give you some idea of what you need to learn:

    function Get-WeekOfMonth{
        Param(
            [datetime]$Date = [datetime]::Today
        )
    
        [int]$offsetDay = $dateTime.Day + $Date.AddDays(-($d.Day-1)).DayOfWeek
        [Math]::Ceiling($offsetDay/7)
    }
    Get-WeekOfMonth
    Get-WeekOfMonth 1/21/2020
    Get-WeekOfMonth (Get-Date)
    Get-WeekOfMonth 'Tuesday, January 21, 2020 1:32:34 AM'

    Unfortunately your date math is incorrect.


    \_(ツ)_/



    • Edited by jrv Tuesday, January 21, 2020 6:46 AM
    Tuesday, January 21, 2020 6:43 AM
  • This is the only completely reliable way to calculate the week of the month.

    function Get-WeekOfMonth{
        Param(
            [datetime]$Date = [datetime]::Today,
            [System.Globalization.CalendarWeekRule]$CalendarRule = 'FirstFullWeek'
        )
    
        $cal = [cultureinfo]::CurrentCulture.Calendar
        $cc = [cultureinfo]::CurrentCulture
        $cal.GetWeekOfYear($date, $CalendarRule, $cc.DateTimeFormat.FirstDayOfWeek)
    }
    Get-WeekOfMonth
    Get-WeekOfMonth 1/21/2020
    Get-WeekOfMonth (Get-Date)
    Get-WeekOfMonth 'Tuesday, January 21, 2020 1:32:34 AM'
    Get-WeekOfMonth 'Tuesday, January 21, 2020 1:32:34 AM' -CalendarRule FirstDay
    The above gets the correct week of the year.  It needs to add a calculation to return the total weeks of the year and adjust the number to the current month.


    \_(ツ)_/



    • Edited by jrv Tuesday, January 21, 2020 7:26 AM
    Tuesday, January 21, 2020 7:12 AM
  • There is also another method that works in a single known culture.

    Get the current week starting day.  Add days to first day of month until day number is found.  I think I remember that this is done simply by getting the modulus of the day divided by two and taking the "floor".  Now subtract from current day and "floor" the results divided by 7.


    \_(ツ)_/


    • Edited by jrv Tuesday, January 21, 2020 7:32 AM
    Tuesday, January 21, 2020 7:31 AM
  • Simple as yours but done correctly this gives the week number counting from the first full week

    function Get-WeekOfMonth{
        Param(
            [datetime]$Date = [datetime]::Today
       )
    
        $d1 = Get-Date $Date -Day 1
        $d2 = $Date.AddDays(7 - $d1.Day)
        [math]::Floor(($d2 - $d1).Days/7)
    }


    \_(ツ)_/

    Tuesday, January 21, 2020 7:47 AM
  • How do I pass this function to a variable?E.g, $week.

    Thursday, January 23, 2020 2:59 AM
  • How do I pass this function to a variable?E.g, $week.

    You can't "pass" a function to a variable.  What does that even mean?

    You can pass a variable to a function.  Is that what you are trying to ask?


    \_(ツ)_/

    Thursday, January 23, 2020 3:27 AM
  • What I mean is, see eg below :

    $month = $file.CreationTime.Month.ToString("00")

    I am giving the variable month a value.

    How do I do the same thing with $week? I want to give the output of the function above to $week.

    Thursday, January 23, 2020 11:32 AM
  • $week =  [cultureinfo]::CurrentCulture.DateTimeFormat.Calendar.GetWeekOfYear( $file.CreationTime.,1,'Sunday')

    \_(ツ)_/

    Thursday, January 23, 2020 11:55 AM
  • Alternate method:

    $week = Get-Date $file.CreationTime -UFormat %V


    \_(ツ)_/

    Thursday, January 23, 2020 12:14 PM