((get-date). day). length

a a 81 Reputation points
2023-10-16T15:21:12.6866667+00:00

Pls, the scripte is not work

I need:

"A" = 1 char Day 1 = is not work

  1. option works > " 2, but, pls. do you have an shorter way .... ?"

I need this format

|=====|

" .. "

" 9."

" 10."

" .. "

cls

1. option, is not work

$xvalue_0 = (get-date).day

$xvalue_1 = $xvalue_0.Length.ToString()

""

if ($xvalue_1 -eq 1) {

# insert one space ?

Write-Host "  ""A""" = 1 char Day (1) = is not work

}

elseif ($xvalue_1 -eq 2) {

# insert to spaces ?

Write-Host "  ""AB""" = 2 char Day (1..2)

}

""

2. option

$xtag = string

" 2. option works > "" " +$xtag.Length+", but, pls. do you have an shorter way .... ?"""

""

"I need this format"

"|=====|"

""" .. """

""" 9."""

""" 10."""

""" .. """

Thanks for your help

Arnold

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,172 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,186 Reputation points
    2023-10-17T02:03:12.0366667+00:00

    I'm not sure I understand what you want.

    The numeric values you're using as examples are represented by objects in memory known as integers. Integers are storage units whose value is stored as a binary value, not the decimal value stored as Arabic numerals.

    Integer objects don't have a length that corresponds to the number of decimal digits in, say, the value 365. You can, however, get the string representation of that binary value. That string will contain the decimal value of the integer.

    If what you want is a string that's one character long when the integers' value is 0 to 9, and a two character string when the integers' value is 10 to 99, and a three character string when the integers' value is from 100 to 999 then all you need to do is ask to have the integer value transformed into a string value.

    Here's an example:

    for ($i=1;$i -le 120; $i+=10){
        "`$i = $i :  `$i Length $($i.length)"
        $n = $i.ToString()
        "`$n = $n : `$n Length $($n.Length)"
    }
    
    

    Run that and you'll get an output that looks like this:

    $i = 1 :  $i Length 1
    $n = 1 : $n Length 1
    $i = 11 :  $i Length 1
    $n = 11 : $n Length 2
    $i = 21 :  $i Length 1
    $n = 21 : $n Length 2
    $i = 31 :  $i Length 1
    $n = 31 : $n Length 2
    $i = 41 :  $i Length 1
    $n = 41 : $n Length 2
    $i = 51 :  $i Length 1
    $n = 51 : $n Length 2
    $i = 61 :  $i Length 1
    $n = 61 : $n Length 2
    $i = 71 :  $i Length 1
    $n = 71 : $n Length 2
    $i = 81 :  $i Length 1
    $n = 81 : $n Length 2
    $i = 91 :  $i Length 1
    $n = 91 : $n Length 2
    $i = 101 :  $i Length 1
    $n = 101 : $n Length 3
    $i = 111 :  $i Length 1
    $n = 111 : $n Length 3
    
    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,186 Reputation points
    2023-10-16T19:08:24.91+00:00

    Use the PowerShell format ("-f") operator:

    $x = get-date "1/1/2023"
    $day = "{0,2:d}" -f $x.day
     1
    
    $x = get-date "1/21/2023"
    $day = "{0,2:d}" -f $x.day
    21
    
    0 comments No comments

  2. a a 81 Reputation points
    2023-10-16T20:09:41.9066667+00:00

    Sir, tnk for your reply !

    $x = get-date "1/1/2023" $day = "{0,2:d}" -f $x.day

    Is this the length from the day date ?

    In need the first day of the month is 1 -eq 1 length

    The 10. days of month have 2 length -eq

    ===========================

    $xtag = string

    $xtag.Length

    ===========================

    (today is the 16. of this Month; is give me "2 length"

    days = (1..9) is -eq 1 space (length) "dayofyear"

    days = (10..99) give me 2 spaces (length) "dayofyear"

    days = (100..364) give me 3 spaces (length) "dayofyear"

    ============================

    If

    elseif

    elseif

    This possibilty is barbare ? non

    =============================

    $xvalue | pipe to $_ -length ..... ?

    Is this possible?

    Thank, Sir

    Arnold

    0 comments No comments

  3. a a 81 Reputation points
    2023-10-16T21:25:55.8366667+00:00
    cls
    ((Get-Date).dayofyear) | Where-Object {($_.Length -eq (1..3))}
    

    Pls Sir

    Wath is i this way? But is not Work and i dont get any error

    Thanks

    Arnold

    0 comments No comments

  4. a a 81 Reputation points
    2023-10-17T02:52:09.82+00:00

    Hello Sir,

    Many thank for your reply !

    My question is

    How get the -lengt from

    ((get-date). day). length ?

    My attention now is:

    cls
    #((Get-Date).dayofyear) | % {($_.Length -eq 1)}
    $test = ((Get-Date).dayofyear)
    $xvalue1 =  (Get-Date).DayofYear
    ""
    if ($xvalue1.ToString().Length -eq 1) {
        Write-Host """A""" = length 1 # (1..9)
        }
    elseif ($xvalue1.ToString().Length -eq 2) {
        Write-Host """AB""" = length 2 # (10..99)
        }
    elseif ($xvalue1.ToString().Length -eq 3) {
        Write-Host """ABC""" = length 3 # (100..999)
    }
    "======================================"
    $xvalue2 =  ((Get-Date).dayofyear) - (([datetime]'12/31' - [datetime]'1/1'))| % days
    if ($xvalue2.ToString().Length -eq 1) {
        Write-Host """A""" = length 1 # (1..9)
        }
    elseif ($xvalue2.ToString().Length -eq 2) {
        Write-Host """AB""" = length 2 # (10..99)
        }
    elseif ($xvalue2.ToString().Length -eq 3) {
        Write-Host """ABC""" = length 3 # (100..999)
    }
    elseif ($xvalue2.ToString().Length -eq 4) {
        Write-Host """ABCD""" = length 4 # (1000..1999)
    }
    "======================================"
    [string]$xvalue3 = "20" #  =  $Xvaleur2
    [string]$xvalue3.Length
    if ($xvalue3 -eq 1) {
        Write-Host """A""" = length 1 # (1)
        }
    elseif ($xvalu3 -eq 2) {
        Write-Host """AB""" = length 2 # (1..2)
        }
    "======================================"
    $xvalue4 = "8" #  =  $Xvaleur2
    if ($xvalue4.ToString().Length-eq 1) {
        Write-Host """A""" = length 1 # (1)
        }
    elseif ($xvalue4.ToString().Length -eq 2) {
        Write-Host """AB""" = length 2 # (1..2)
        }
    "======================================"
    $xvalue5 = [string][string](get-date).day
    if ($xvalue5.ToString().Length-eq 1) {
        Write-Host """A""" = length 1 # (1..2)
        }
    elseif ($xvalue5.ToString().Length-eq 2) {
        Write-Host """AB""" = length 2 # (1..2)
        }
    "======================================"
    
    "ABC" = length 3
    ======================================
    "ABCD" = length 4
    ======================================
    2
    ======================================
    "A" = length 1
    ======================================
    "AB" = length 2
    ======================================
    

    I can développe this by switch also:

    now: the 1. "ABC" has length 3 = is the #290 day of this year = length 3

    Explication: 1. of the year have 1 length,

    the 10 of the year have 2 length

    and also 364 have 3 length

    I have finde only all length possible for the Calender

    Rigth , it's work only my scripte is very poor (Barbare) :(

    https://docs.google.com/spreadsheets/d/16Rn_c8yBqzGKYwYkzXERmNM_O9EkIHoLLxYRAtOt2_Y/edit?pli=1#gid=2

    Many Thank Sir

    Arnold