Bizarre Day() feature

Discussion in 'ASP General' started by jethro_uk@hotmail.com, Jul 13, 2006.

  1. Guest

    Hi all,

    I'm having a hard time with the Day() function. Basically, it appears
    to be picking and choosing which part of a date string it uses to
    return the result ...

    heres some code :

    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></TITLE>
    </HEAD>
    <BODY>
    <%
    Function formatDate(dDate)

    formatDate=""
    If IsNull(dDate) or Trim(dDate)="" Then
    Else If IsDate(dDate) Then
    If dDate<CDate("January 1, 1901") Then
    formatDate=" - - "
    Else formatDate=Day(dDate) & " " & MonthName(Month(dDate), true) &
    " " & year(dDate)
    End If
    End If
    End If
    If formatDate="1 Jan 1900" Then
    formatDate="n/a"
    End If
    End Function

    dim sDate1,sDate2

    sDate1="12-31-06"
    sDate2="07/11/2006"
    sDate2="26-12-2006"


    Response.Write ("DEFAULT Session.LCID=" & Session.LCID)
    Response.Write ("<BR/>")
    Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    formatDate(sdate1) & "'")
    Response.Write ("<BR/>")
    Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    formatDate(sdate2) & "'")
    session.LCID=1033
    Response.Write ("<BR/>")
    Response.Write ("<BR/>")
    Response.Write ("Session.LCID=" & Session.LCID)
    Response.Write ("<BR/>")
    Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    formatDate(sdate1) & "'")
    Response.Write ("<BR/>")
    Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    formatDate(sdate2) & "'")
    session.LCID=2057
    Response.Write ("<BR/>")
    Response.Write ("<BR/>")
    Response.Write ("Session.LCID=" & Session.LCID)
    Response.Write ("<BR/>")
    Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    formatDate(sdate1) & "'")
    Response.Write ("<BR/>")
    Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    formatDate(sdate2) & "'")
    %>
    <P>&nbsp;</P>

    </BODY>
    </HTML>

    OUTPUT################################
    DEFAULT Session.LCID=2057
    Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'

    Session.LCID=1033
    Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'

    Session.LCID=2057
    Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'
    /OUTPUT###################################

    Notice how it uses the FIRST part of sDate1 to return the day, but the
    SECOND part of sDate2

    I have tried fiddling with the Locale settings, but whatever they are,
    Day("12/26/2006") always returns "26".

    Can anyone explain, and hopefully suggest what to do ?

    Thanks in advance
    , Jul 13, 2006
    #1
    1. Advertising

  2. Patrice Guest

    This is expected 12/26/2006 is always thought to be day 26 of month 12
    because it can't be day 12 of month 26...

    Note that the problem doesn't lie really with the Day function itself. The
    problem is that Day should use a date parameter not a string. As you are
    providing a string to this function, the string is first converted to a date
    (and it can't be anything else than day 26 month 12). The day is just then
    extracted from the date. As a result it always return 26.

    Try rather with a date such as 02/03 that can be day 2 of month 3 or day 3
    of month 2.

    Also my personal preference is to work always with the proper datatype i.e.
    to convert a user input string as soon as possible to a real date variable
    (or to use a real date variable from the beginning if this is not a user
    input)...

    --
    Patrice

    <> a écrit dans le message de news:
    ...
    > Hi all,
    >
    > I'm having a hard time with the Day() function. Basically, it appears
    > to be picking and choosing which part of a date string it uses to
    > return the result ...
    >
    > heres some code :
    >
    > <HTML>
    > <HEAD>
    > <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    > <TITLE></TITLE>
    > </HEAD>
    > <BODY>
    > <%
    > Function formatDate(dDate)
    >
    > formatDate=""
    > If IsNull(dDate) or Trim(dDate)="" Then
    > Else If IsDate(dDate) Then
    > If dDate<CDate("January 1, 1901") Then
    > formatDate=" - - "
    > Else formatDate=Day(dDate) & " " & MonthName(Month(dDate), true) &
    > " " & year(dDate)
    > End If
    > End If
    > End If
    > If formatDate="1 Jan 1900" Then
    > formatDate="n/a"
    > End If
    > End Function
    >
    > dim sDate1,sDate2
    >
    > sDate1="12-31-06"
    > sDate2="07/11/2006"
    > sDate2="26-12-2006"
    >
    >
    > Response.Write ("DEFAULT Session.LCID=" & Session.LCID)
    > Response.Write ("<BR/>")
    > Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    > formatDate(sdate1) & "'")
    > Response.Write ("<BR/>")
    > Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    > formatDate(sdate2) & "'")
    > session.LCID=1033
    > Response.Write ("<BR/>")
    > Response.Write ("<BR/>")
    > Response.Write ("Session.LCID=" & Session.LCID)
    > Response.Write ("<BR/>")
    > Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    > formatDate(sdate1) & "'")
    > Response.Write ("<BR/>")
    > Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    > formatDate(sdate2) & "'")
    > session.LCID=2057
    > Response.Write ("<BR/>")
    > Response.Write ("<BR/>")
    > Response.Write ("Session.LCID=" & Session.LCID)
    > Response.Write ("<BR/>")
    > Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    > formatDate(sdate1) & "'")
    > Response.Write ("<BR/>")
    > Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    > formatDate(sdate2) & "'")
    > %>
    > <P>&nbsp;</P>
    >
    > </BODY>
    > </HTML>
    >
    > OUTPUT################################
    > DEFAULT Session.LCID=2057
    > Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    > Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'
    >
    > Session.LCID=1033
    > Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    > Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'
    >
    > Session.LCID=2057
    > Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    > Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'
    > /OUTPUT###################################
    >
    > Notice how it uses the FIRST part of sDate1 to return the day, but the
    > SECOND part of sDate2
    >
    > I have tried fiddling with the Locale settings, but whatever they are,
    > Day("12/26/2006") always returns "26".
    >
    > Can anyone explain, and hopefully suggest what to do ?
    >
    > Thanks in advance
    >
    Patrice, Jul 13, 2006
    #2
    1. Advertising

  3. wrote:
    > Hi all,
    >
    > I'm having a hard time with the Day() function. Basically, it appears
    > to be picking and choosing which part of a date string it uses to
    > return the result ...
    >

    To add to what Patrice said: always use a standard date format such as
    the ISO standard YYYY-MM-DD

    You can use UI gadgets such as calendar controls or dropdown controls to
    enforce the standard format.
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jul 13, 2006
    #3
  4. <> wrote in message
    news:...
    > Hi all,
    >
    > I'm having a hard time with the Day() function. Basically, it appears
    > to be picking and choosing which part of a date string it uses to
    > return the result ...
    >
    > heres some code :


    Enclose a Date 'string' with #

    like this

    a = CDate(#31/12/2006#)

    This is a rarity in VBS, not known by many scripters.

    Of course, in my case, the date format (dd-mm-yyyy) between # # is
    according to my own locale.

    > <HTML>
    > <HEAD>
    > <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    > <TITLE></TITLE>
    > </HEAD>
    > <BODY>
    > <%
    > Function formatDate(dDate)
    >
    > formatDate=""
    > If IsNull(dDate) or Trim(dDate)="" Then
    > Else If IsDate(dDate) Then
    > If dDate<CDate("January 1, 1901") Then
    > formatDate=" - - "
    > Else formatDate=Day(dDate) & " " & MonthName(Month(dDate), true) &
    > " " & year(dDate)
    > End If
    > End If
    > End If
    > If formatDate="1 Jan 1900" Then
    > formatDate="n/a"
    > End If
    > End Function
    >
    > dim sDate1,sDate2
    >
    > sDate1="12-31-06"
    > sDate2="07/11/2006"
    > sDate2="26-12-2006"
    >
    >
    > Response.Write ("DEFAULT Session.LCID=" & Session.LCID)
    > Response.Write ("<BR/>")
    > Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    > formatDate(sdate1) & "'")
    > Response.Write ("<BR/>")
    > Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    > formatDate(sdate2) & "'")
    > session.LCID=1033
    > Response.Write ("<BR/>")
    > Response.Write ("<BR/>")
    > Response.Write ("Session.LCID=" & Session.LCID)
    > Response.Write ("<BR/>")
    > Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    > formatDate(sdate1) & "'")
    > Response.Write ("<BR/>")
    > Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    > formatDate(sdate2) & "'")
    > session.LCID=2057
    > Response.Write ("<BR/>")
    > Response.Write ("<BR/>")
    > Response.Write ("Session.LCID=" & Session.LCID)
    > Response.Write ("<BR/>")
    > Response.Write ("Date1=" & sdate1 & ". formatDate(" & sdate1 & ") ='" &
    > formatDate(sdate1) & "'")
    > Response.Write ("<BR/>")
    > Response.Write ("Date2=" & sdate2 & ". formatDate(" & sdate2 & ") ='" &
    > formatDate(sdate2) & "'")
    > %>
    > <P>&nbsp;</P>
    >
    > </BODY>
    > </HTML>
    >
    > OUTPUT################################
    > DEFAULT Session.LCID=2057
    > Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    > Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'
    >
    > Session.LCID=1033
    > Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    > Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'
    >
    > Session.LCID=2057
    > Date1=12-31-06. formatDate(12-31-06) ='31 Dec 2006'
    > Date2=26-12-2006. formatDate(26-12-2006) ='26 Dec 2006'
    > /OUTPUT###################################
    >
    > Notice how it uses the FIRST part of sDate1 to return the day, but the
    > SECOND part of sDate2
    >
    > I have tried fiddling with the Locale settings, but whatever they are,
    > Day("12/26/2006") always returns "26".
    >
    > Can anyone explain, and hopefully suggest what to do ?
    >
    > Thanks in advance
    >
    Egbert Nierop \(MVP for IIS\), Jul 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elliot M. Rodriguez

    Bizarre Debugging Problem

    Elliot M. Rodriguez, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    292
    Elliot M. Rodriguez
    Nov 19, 2003
  2. Michael Carr

    Bizarre loss of Session variables

    Michael Carr, Jan 13, 2004, in forum: ASP .Net
    Replies:
    14
    Views:
    714
    Michael Carr
    Jan 14, 2004
  3. Andy
    Replies:
    1
    Views:
    678
  4. Replies:
    2
    Views:
    262
  5. Rob Redmon

    Convert day of year to month, day

    Rob Redmon, Apr 4, 2008, in forum: Ruby
    Replies:
    7
    Views:
    278
    Alex Wayne
    Apr 4, 2008
Loading...

Share This Page