Date format

Discussion in 'C#' started by wintersun, Aug 25, 2006.

  1. #1
    Is there any simple way of getting the month in 2 digit format when using the month function?

    e.g. I type month(now) and it returns 8, not 08.

    Is it possible to change this default setting or do I have to use an if statement to determine if it's one digit or two and add a 0 if its one?

    Or even better, is there a way to format date in yyyy-mm-dd that I have missed?

    Just thinking there should be an easier way.

    //Anders
     
    wintersun, Aug 25, 2006 IP
  2. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    sure.

    code below will do both jobs for ya in one fell swoop.

    
    
    dim zmonth, zdate, zyear, zdateformat
    
    zmonth = cstr(month(now))
    zdate = cstr(date(now))
    zyear = cstr(year(now))
    
    if len(zmonth) = 1 then
    zmonth = "0" & zmonth
    end if
    
    if len(zdate) = 1 then
    zdate= "0" & zdate
    end if
    
    zdateformat = zyear & "-" & zmonth & "-" & zdate
    response.write zdateformat
    
    
    Code (markup):
     
    vectorgraphx, Aug 25, 2006 IP
  3. wintersun

    wintersun Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, it's a bit shorter than my code, byt was just thinking if there was a way to do it without the "if"s, seems a bit unnessecary to me that you can't get it directly out of the date string ;)
     
    wintersun, Aug 26, 2006 IP
  4. Link.ezer.com

    Link.ezer.com Peon

    Messages:
    647
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if statement is good. ;)

    But without if .... you may use Loop

    code:

    For i=1 to ezerLength - Len(sString)
    sString="0"&sString
    next


    source code from: http://3w.ezer.com/asp/time/now.asp#example_code

    :)
     
    Link.ezer.com, Aug 26, 2006 IP
  5. ohidur

    ohidur Member

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    U may Use : Format(Now, "mm-dd-yyyy")
    It May Work. If Language - is VB Related.
     
    ohidur, Aug 27, 2006 IP
  6. BurgerKing

    BurgerKing Active Member

    Messages:
    397
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    58
    #6
    how about

    response.write(right$("0" & month(now()),2))

    the
    Right$("0" & Month(Now()), 2)
    is what you want
     
    BurgerKing, Aug 27, 2006 IP
  7. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I was curious about this and tried it out myself - couldnt get it to work. I played around with the "right" function and made a slight modification to burgerking's code - (and by slight, i mean i deleted one character - LOL) and got it to work right by using this:

    right("0" & month(now()),2)

    it is hands down a far more elegant solution than mine, and i'll be using it myself from now on in similar coding situations. kudos burgerking!

    VG
     
    vectorgraphx, Aug 30, 2006 IP
  8. BurgerKing

    BurgerKing Active Member

    Messages:
    397
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    58
    #8
    The right$ is for vb.net & vb6 - not sure about asp - sorry :)

    for asp.net you can also use strings.right

    Glad you like the solution :)
     
    BurgerKing, Aug 31, 2006 IP