using date in ASP

Discussion in 'C#' started by ludwig, Jul 13, 2008.

  1. #1
    Hi guys, another quick tutorial on using ASP dates

    • Date formats
    • Date Parts
    • Date Add and Subtract
    • Date Difference
    • Is this year a Leap year
    • How to calculate Julian Date
    • Date countdown till a given date
    • First and Last day of Month

    Date Format [ Relative to the date as 2/7/2000 10:37:48 AM ]
    <%=FormatDateTime(Now(),1) %>
    HTML:
    Outputs as : Monday, February 07, 2000

    <%=FormatDateTime(Now(),2)%>
    HTML:
    Outputs as : 2/7/2000

    <%=Now%>
    HTML:
    Outputs as : 2/7/2000 10:24:57 AM

    Date Part [ Relative to the date 2/7/2000 10:37:48 AM ]

    Day

    Today is <%=Day(Date)%>
    Outputs as : Today is 7

    Week

    This is week # <%= DatePart("ww",date)%> of the this year
    Outputs as : This is week # 7 of this year


    This is weekday <%= WeekDay(Date)%> of this year
    Outputs as : This is weekday 2 of this year


    Today is <%=WeekDayName(WeekDay(Date))%>
    Outputs as : Today is Monday


    Month


    The month Now is <%=Month(Date)%>
    Outputs : The month Now is 2

    Another way, of saying the current month is <%=(DatePart("m",date))%>
    Outputs : Another way, of saying the current month is 2

    The MonthName is <%=MonthName(Month(Date))%>
    Outputs : The MonthName is February

    Year

    The year is <%=Year(Date)%>
    Outputs : The year is 2000

    Date Add and Subtract

    Add

    Day

    2 Days from 2/9/2000 it will be <%=DateAdd("d",2,CDate("2/9/2000"))%>
    Outputs : 2 Days from now it will be 2/11/2000

    Month

    2 Months from 2/9/2000 it will be <%=DateAdd("m",2,CDate("2/9/2000"))%>
    Outputs : 2 Months from 2/9/2000 it will be 4/9/2000

    Year

    2 Years from 2/9/2000 it will be <%=DateAdd("yyyy",2,CDate("2/9/2000"))%>
    Outputs : 2 Years from 2/9/2000 it will be 2/9/2002

    Subtract

    Day

    2 Days back from 2/9/2000 it was <%=DateAdd("d",-2,CDate("2/9/2000"))%>
    Outputs : 2 Days back from now it was be 2/7/2000

    Month

    2 Months back from 2/9/2000 it was be <%=DateAdd("m",-2,CDate("2/9/2000"))%>
    Outputs : 2 Months back from 2/9/2000 it was 12/9/1999

    Year

    2 Years from 2/9/2000 it will be <%=DateAdd("yyyy",-2,CDate("2/9/2000"))%>
    Outputs : 2 Years back from 2/9/2000 it was 2/9/1998

    Date Difference [ Relative Date : 2/7/2000 ]

    Day

    Its <%=DateDiff("d","2/7/2000","8/15/1947")*-1 %> days between '8/15/1947' and '2/7/2000'
    Outputs : Its 19169 days between '8/15/1947' and '2/7/2000'

    Week

    Its <%=DateDiff("ww","2/7/2000","8/15/1947")*-1 %> weeks between '8/15/1947' and '2/7/2000'
    Outputs : Its 2739 weeks between '8/15/1947' and '2/7/2000'

    Month

    Its <%=DateDiff("m","2/7/2000","8/15/1947")*-1 %> months between '8/15/1947' and '2/7/2000'
    Outputs : Its 630 months between '8/15/1947' and '2/7/2000'

    Quarter

    Its <%=DateDiff("q","2/7/2000","8/15/1947")*-1 %> quarters between '8/15/1947' and '2/7/2000'
    Outputs : Its 210 Quarters between '8/15/1947' and '2/7/2000'

    Year

    Its <%=DateDiff("yyyy","2/7/2000","8/15/1947")*-1 %> years between '8/15/1947' and '2/7/2000'
    Outputs : Its 53 between '8/15/1947' and '2/7/2000'

    Leap Year

    <%
    Dim lYear
    lYear      = Year(CDate("2/9/2000"))
    leap_yes = "is a leap year"
    leap_no   = "is not a leap year"
    If (lYear mod 4 = 0) AND ((lYear mod 1  0) OR (lYear mod 4 = 0)) then
    ' It is a Leap Year
    Response.Write lYear & " " & leap_yes
    else
    ' it is not a leap year
    Response.Write lYear & " " & leap_no
    End if
    %>
    HTML:
    Outputs : 2000 is a leap year

    Julian Date

    <%
    d0=DateSerial ("2000", "1", "1")
    d1=Date - d0 + 1
    Response.write "Today's Julian Date Is #" & d1
    %> 
    HTML:
    Outputs : Today's Julian Date Is #38

    Date Countdown

    <%
    dim strDateTime
    strDateTime =   CDate("2/9/2000")
    strFutureDay = #12/31#
    Response.write "There are" & INT(strFutureDay - strDateTime) & " more days till December 31st."
    %> 
    HTML:
    Outputs : There are 327 more days till December 31st.

    How to find first and last day of the month

    Calculate first date of current month

    <%
    BaseDate = CDate("2/9/2000")
    FirstOfMonth = DateSerial(Year(BaseDate), Month(BaseDate) + iOffset, 1)
    Response.write "The first date of this month is " & FirstOfMonth
    %>
    
    HTML:
    Outputs : The first date of this month is 2/1/2000


    Calculate last date of current month

    <%
    BaseDate = CDate("2/9/2000")
    EndOfMonth = DateSerial(Year(BaseDate), Month(BaseDate) + 1, 0)
    Response.write "The last date of this month is " & EndOfMonth
    %>
    HTML:
    Outputs : The last date of this month is 2/29/2000
     
    ludwig, Jul 13, 2008 IP
  2. tomred

    tomred Peon

    Messages:
    382
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Thanks for this..

    Any chance you could write a simple fucntion that displays the date in UK format dd/mm/yyyy
     
    tomred, Jul 15, 2008 IP
  3. ludwig

    ludwig Notable Member

    Messages:
    2,253
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    225
    #3
    try this
    Dim d_today,var_Locale
    var_Locale = SetLocale(2057) ' UK
    d_today=Now ' Along with time
    Response.Write d_today
    HTML:
     
    ludwig, Jul 15, 2008 IP