Article: ASP DateTime Displaying friendly asp dates.

ASP DateTime  Displaying friendly asp dates.

Article:
ASP DateTime Displaying friendly asp dates.

Posted by: Carlos on 16/06/2008 21:32:24
Star Rating of 1. Actual Rating of: 4
 You must be logged in to vote on thread ratings
Displaying times in asp is quite a fiddly affair. Theres a lot of different ways of displaying ASP dates and times. Using FormatDateTime you can output data as just the date, just the time, a mixture, 24 hour time format, 12 hour time format etc etc. One very useful tip for doing this in projects is to create yourself a little function such as the one found below.

ASP DateTime - Displaying friendly asp dates.

Displaying times in asp is quite a fiddly affair. Theres a lot of different ways of displaying ASP dates and times. Using FormatDateTime you can output data as just the date, just the time, a mixture, 24 hour time format, 12 hour time format etc etc. One very useful tip for doing this in projects is to create yourself a little function such as the one found below. This gives you alot of flexibility and scope to easily change formats and display them as you wish.

Our function here, covers one of the most common formatting issues, often seen in forums and blogs. It simply changes any date that is within the past 24 hours to the word 'Today' followed by the time, and replaces anything >24 hours and less than 48< hours with the word 'Yesterday' followed by the time.

Our function is called as follows:

<%
DateString=rs("Timestamp") '--Any date source such as a timestamp from your database data.
Response.write(FriendlyDates(DateString,Session("ID"),0))
%>


<%function FriendlyDates(DateTime,LoggedIn,Format)
    if LoggedIn>0 then   
        DateTime=DateAdd("h",session("Timezone"),DateTime)
        if Format=0 then '//Standard Date + Time unless within the last 48hours.
            if DateDiff("d",DateTime,Date) < 1 then FriendlyDates="Today at "&cStr(FormatDateTime(DateTime,3))
            if DateDiff("d",DateTime,Date) = 1 then FriendlyDates="Yesterday at "&cStr(FormatDateTime(DateTime,3))
            if DateDiff("d",DateTime,Date) > 1 then FriendlyDates=DateTime
        end if
    else
        FriendlyDates=DateTime
    end if
    FriendlyDates="<span class=""smallTimeStd"">"&FriendlyDates&"</span>"
end function
%>


The function again with no scope for timezones or formatting:

<%
DateString=rs("Timestamp") '--Any date source such as a timestamp from your database data.
Response.write(FriendlyDates(DateString))
%>

<%function FriendlyDates(DateTime)
    if DateDiff("d",DateTime,Date) < 1 then FriendlyDates="Today at "&cStr(FormatDateTime(DateTime,3))
    if DateDiff("d",DateTime,Date) = 1 then FriendlyDates="Yesterday at "&cStr(FormatDateTime(DateTime,3))
    if DateDiff("d",DateTime,Date) > 1 then FriendlyDates=DateTime
    FriendlyDates="<span class=""smallTimeStd"">"&FriendlyDates&"</span>"
end function
%>


Theres a few parts to this function that you may not use, but weve left them in just incase you want to patch around it. For example our site stores a session("Timezone") that is an integer between -12 and 12. This is set when a user logs in, and is based off their profile information. This allows us to display times relative to their global location.

The 3rd parameter is passed as a 0. This is purely incase we want to change the format, or add different formatting styles later on.
For example we could add a 'If Format=1 then' where the code never shows the time, and only provides the date. In a chatroom you may want to add an if statement to only show the time.

Our example outputs the time in a span tag with a class. This enables you to add a CSS class so that all your websites dates and times show in exactly the same style.

Thats about it for this quick little article. Hopefully you'll be able to integrate this ASP Date formatting technique into your own work!

Happy programming.

Comments board:

There are no comments yet. Be the first to post!
web-affinity works best with Firefox.
We recommend all users browse our sites in Firefox.
Gr00pz © 2006-2008
         
   Art-Affinity   |    Mac-Affinity   |    Gadget-Affinity   |    PCGamer   |   Rules FAQ Network 
Web,developer,website,forums,community,asp,php,html,css,j4vascript,help