how to add 2 hours to code which shows server time in asp page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karen987
    New Member
    • Mar 2007
    • 114

    how to add 2 hours to code which shows server time in asp page

    This code is for a comments section on a news article. Each comment shows the time. The page is an asp page.

    I'd like to add 2 hours, hopefully within the code below to make the time show 2 hours ahead, i'd need to be able to adjust it for daylight saving in future.

    This code:
    Code:
    <%=FormatDateTime(C_DATE,3)%>
    shows the time like this

    "4:34:20 AM"



    Just to clarify the layout: The comments are added to the end of the article, by readers, and they open up in a separate small window from a link . Headlines of the comments show below the news article. Each headline shows the title, and date and time posted.

    In other words, on the public side, i'd like the comments to show another time, 2 hours ahead. I don't mind if the database contains the orignal server time, since it won't show on the public sides.

    Any tips anyone?
    Thanks in advance

    --------------------




    Code:
      SQL = "SELECT ID, fldNAME, fldCOMMENT, fldDATE, fldSUBJECT, fldM_ID, fldCITY, fldCOUNTRY, fldALLOW, fldEMAIL FROM nm_tbl_comment WHERE fldNEWS_ID = " & NID & " ORDER BY ID ASC" 
                                Set RS = Server.CreateObject("ADODB.Recordset") 
                                RS.LockType   = 1 
                                RS.CursorType = 0 
                                RS.Open SQL, MyConn     
                                     WHILE NOT RS.EOF 
                                        CCOUNT    = CCOUNT + 1 
                                          C_ID      = trim(RS("ID")) 
    
                                        C_NAME    = trim(RS("fldNAME")) 
                                        C_COMMENT = trim(RS("fldCOMMENT")) 
                                     C_SUBJECT = trim(RS("fldSUBJECT")) 
                                     C_M_ID    = trim(RS("fldM_ID")) 
                                        C_DATE    = trim(RS("fldDATE")) 
                                     C_CITY    = trim(RS("fldCITY")) 
                                     C_COUNTRY = trim(RS("fldCOUNTRY")) 
                                     C_ALLOW_E = trim(RS("fldALLOW"))     
                                     C_EMAIL = trim(RS("fldEMAIL"))     
                                     
                                        %>     
                 
                 
                 
                 
                 
                                        <td width="100%"><%IF Trim(C_ALLOW_E) = "1" THEN%><a class="linkComment" href="file/_mail.asp?ID=<%=C_ID%>&AID=<%=NID%>" onClick="NewWindow(this.href,'name','450','410','Yes');return false;"> 
                                        <img src="blogs/img_ss.gif" width="14" height="9" alt="" border="0" /></a> <%END IF%> <span style="color: #4169E1; font-size: 10px"><%= C_NAME %> - <%=C_CITY%>-<%=C_COUNTRY%> - <%=FormatDateTime(C_DATE,2)%>-<%=FormatDateTime(C_DATE,3)%> </span></td>
  • karen987
    New Member
    • Mar 2007
    • 114

    #2
    I have a news weblog with asp pages. I need to make a system wide change so that the articles are showing 2 hours ahead in the public view. This is because the server is in another country. I'd need to be able to adjust for daylight saving.

    I think the common file for this is the include file "config.asp ", and i have included the code below.

    Here is a page example which shows the political news in posted date order, latest on top. Call it "page.asp"


    Code:
    <%Dim iStart, iOffset, SQL, RS, EOF_VAL, strRETURNED_DATA, iRows, iCols, iStop, iRowLoop, strPREV_LINK, strNEXT_LINK, CID, CNAME, AUTHOR, NID, SUMMARY, IMAGE, CATEGORIES, TITLE, POSTED, XI 
    
        CID     = Trim(Request.QueryString("CID")) 
        CNAME = Request("CNAME") 
        iStart  = Request.QueryString("Start") 
        iOffset = Request.QueryString("Offset") 
         
         
        IF IS_VALID_ID(CID) THEN 
         
            If Not IsNumeric(iStart) or Len(iStart) = 0 then 
              iStart = 0 
            Else 
              iStart = CInt(iStart) 
            End If 
            If Not IsNumeric(iOffset) or Len(iOffset) = 0 then 
              iOffset = ALL_ARTICLES_PAGE_SIZE 
            Else 
              iOffset = Cint(iOffset) 
            End If     
             
            IF DB_TO_USE = 1 OR DB_TO_USE = 3 THEN     
                SQL = "SELECT nm_tbl_news.ID AS NID, nm_tbl_news.fldSUMMARY AS SUMMARY, nm_tbl_news.fldIMAGE AS NIMAGE, nm_tbl_news.fldTITLE AS TITLE, nm_tbl_agent.fldNAME AS ANAME, nm_tbl_news.fldPOSTED AS POSTED FROM nm_tbl_news, nm_tbl_agent WHERE (nm_tbl_agent.ID = nm_tbl_news.fldAID) AND (nm_tbl_news.fldACTIVE=1) AND (Now() BETWEEN fldPOSTED AND fldEXPIRES) AND (nm_tbl_news.ID IN (SELECT fldNEWS_ID FROM nm_tbl_news_cate WHERE fldCATE_ID = " & CID & ")) ORDER BY nm_tbl_news.ID DESC" 
            ELSE 
                SQL = "SELECT nm_tbl_news.ID AS NID, nm_tbl_news.fldSUMMARY AS SUMMARY, nm_tbl_news.fldIMAGE AS NIMAGE, nm_tbl_news.fldTITLE AS TITLE, nm_tbl_agent.fldNAME AS ANAME, nm_tbl_news.fldPOSTED AS POSTED FROM nm_tbl_news, nm_tbl_agent WHERE (nm_tbl_agent.ID = nm_tbl_news.fldAID) AND (nm_tbl_news.fldACTIVE=1) AND (GetDate() BETWEEN fldPOSTED AND fldEXPIRES) AND (nm_tbl_news.ID IN (SELECT fldNEWS_ID FROM nm_tbl_news_cate WHERE fldCATE_ID = " & CID & ")) ORDER BY nm_tbl_news.ID DESC" 
            END IF 
             
            Call OPEN_DB() 
             
            Set RS = MyConn.Execute(SQL) 
            IF NOT RS.EOF THEN 
                strRETURNED_DATA = RS.getrows 
                EOF_VAL = False 
            ELSE 
                EOF_VAL = True     
            END IF     
            RS.close 
            Set RS = Nothing 
            ' Get settings 
            Dim DATE_F 
            DATE_F = GET_SETTINGS(False, "fldDATE_F") 
           
             
            IF not EOF_VAL = True THEN 
                iRows = UBound(strRETURNED_DATA, 2) 
                iCols = UBound(strRETURNED_DATA, 1) 
                If iRows > (iOffset + iStart) Then 
                   iStop = iOffset + iStart - 1 
                Else 
                   iStop = iRows 
                End If             
                FOR iRowLoop = iStart to iStop 
                    NID = strRETURNED_DATA(0, iRowLoop) 
                    SUMMARY = strRETURNED_DATA(1, iRowLoop) 
                    IMAGE = strRETURNED_DATA(2, iRowLoop) 
                    TITLE = strRETURNED_DATA(3, iRowLoop) 
                    AUTHOR = strRETURNED_DATA(4, iRowLoop) 
                    POSTED = strRETURNED_DATA(5, iRowLoop) 
                    TITLE = PROCESS_SHORTCUTS(False, TITLE) 
                    SUMMARY = PROCESS_SHORTCUTS(False, SUMMARY)     
                    CATEGORIES = GET_CATES(NID)         
                                %> 
    
    
                      <table cellpadding="2" cellspacing="0" border="0" width="100%"><tr> 
                          <td><div align="justify"><a href="page.asp?ID=<%=NID%>" class="aTITLE"><%= TITLE %></a></div></td> 
                    </tr> 
                    <tr><td colspan="2" class="divPOSTEDON"><div align="justify"><%= FormatDateTime(POSTED,DATE_F) %> 
                      <% IF SHOW_AUTHOR = True THEN %> 
       By:<%= AUTHOR %> 
    <% Else %> 
      
    <% End If %> 
                    </div></td></tr> 
                   <tr><td colspan="2" class="tdSUMMARY"><div align="justify"> 
                        <%IF NOT (IMAGE = "" OR IsNull(IMAGE)) THEN%> 
                          <img src="<%=IMAGE%>" width="50" height="50" border="1" align="left" /> 
                      <% END IF %> 
                      <%= SUMMARY %></div></td></tr> 
                    </table> 
                     <div align="justify"><hr width="100%" size="1" style="color: gray;height: 1px;width: 100%;" /> 
                       
                          <% 
                NEXT 
                ' Close DB         
                MyConn.close             
                Set MyConn = Nothing         
                 
                If iStart > 0 Then 
                 strPREV_LINK = "<A class=""clsPAGING"" HREF=""politics.asp?Start=" & iStart-iOffset & "&Offset=" & iOffset & "&CID=" & CID & """>Previous " & iOffset & "</A>" 
                Else 
                    strPREV_LINK = " " 
                End If     
                If iStop < iRows Then 
                 strNEXT_LINK = " <A class=""clsPAGING"" HREF=""politics.asp?Start=" & iStart+iOffset & "&Offset=" & iOffset & "&CID=" & CID & """>Next " & iOffset & "</A>" 
                Else         
                 strNEXT_LINK = " "     
                End If %>                     
             
                <table width="100%" align="center" cellpadding="2" cellspacing="0" border="0"><tr class="trPAGING"> 
                 <td width="50%" align="left"><%=strPREV_LINK%></td> 
                 <td width="50%" align="right"><%=strNEXT_LINK%></td> 
                </tr></table>     
                     
            <%ELSE%>     
             
                    <table width="100%" align="center" cellpadding="2" cellspacing="0" border="0"><tr><td>No political news yet</td></tr></table> 
             
            <%END IF%>         
        <%END IF%>

    and i believe this is the common code which is used for all the dates, and it's in the include file which is included in the header file and it's called "config.asp ". I have only shown the code which i think is relevant to the dating of articles below:

    Is there something i can add below to make a system wide change of 2 hours ahead? I'd need to be able to adjust for daylight, so i presume it's a matter of changing only the number for the hours somewhere.


    config.asp


    Code:
       ' Assemble date value 
        FUNCTION ASEMBLE_DATE_FORMAT(sDAY, sMONTH, sYEAR) 
            Dim DATE_FORMAT, sDATE                   
             
            DATE_FORMAT = 1 ' US Locale 
             
            SELECT CASE DATE_FORMAT 
                CASE 1 
                    sDATE = sMONTH & "/" & sDAY  & "/" & sYEAR 
                CASE ELSE 
                    sDATE = sMONTH & "/" & sDAY  & "/" & sYEAR 
            END SELECT             
            ASEMBLE_DATE_FORMAT = sDATE 
        END FUNCTION 
         
            
           
        END SUB               
    %>
    and this

    It does actually say, that there is no need to modify it, but that may be because the other things on the config.asp page are customisable, ie. email, and number of articles etc.

    config.asp


    Code:
    <% '// NO NEED TO MODIFY ANYTHING BELOW 
    FUNCTION CNT_DATE(sDATE) 
      IF IsDate(sDATE) = True THEN 
        SELECT CASE DATE_FORMAT 
            CASE 1        'YYYYMMDD 
                CNT_DATE = Year(sDATE) & Right(Cstr(Month(sDATE) + 100),2) & Right(Cstr(Day(sDATE) + 100),2) 
            CASE 2        'YYYY-MM-DD 
                CNT_DATE = Year(sDATE) & "-" & Right(Cstr(Month(sDATE) + 100),2) & "-" & Right(Cstr(Day(sDATE) + 100),2) 
            CASE 101    'mm/dd/yy 
                CNT_DATE = Right(Cstr(Month(sDATE) + 100),2) & "/" & Right(Cstr(Day(sDATE) + 100),2) & "/" & Right(Cstr(Year(sDATE)),2) 
            CASE 102    'yy.mm.dd 
                CNT_DATE = Right(Cstr(Year(sDATE)),2) & "." & Right(Cstr(Month(sDATE) + 100),2) & "." & Right(Cstr(Day(sDATE) + 100),2) 
            CASE 103    'dd/mm/yy 
                CNT_DATE = Right(Cstr(Day(sDATE) + 100),2) & "/" & Right(Cstr(Month(sDATE) + 100),2) & "/" & Right(Cstr(Year(sDATE)),2) 
            CASE 104    'dd.mm.yy 
                CNT_DATE = Right(Cstr(Day(sDATE) + 100),2) & "." & Right(Cstr(Month(sDATE) + 100),2) & "." & Right(Cstr(Year(sDATE)),2) 
            CASE 105    'dd-mm-yy 
                CNT_DATE = Right(Cstr(Day(sDATE) + 100),2) & "-" & Right(Cstr(Month(sDATE) + 100),2) & "-" & Right(Cstr(Year(sDATE)),2) 
            CASE 110    'mm-dd-yy 
                CNT_DATE = Right(Cstr(Month(sDATE) + 100),2) & "-" & Right(Cstr(Day(sDATE) + 100),2) & "-" & Right(Cstr(Year(sDATE)),2) 
            CASE 111    'yy/mm/dd 
                CNT_DATE = Right(Cstr(Year(sDATE)),2) & "/" & Right(Cstr(Month(sDATE) + 100),2) & "/" & Right(Cstr(Day(sDATE) + 100),2)     
            CASE 112    'yymmdd 
                CNT_DATE = Right(Cstr(Year(sDATE)),2) & Right(Cstr(Month(sDATE) + 100),2) & Right(Cstr(Day(sDATE) + 100),2) 
        END SELECT     
      ELSE 
          CNT_DATE = Null 
      END IF 
    END FUNCTION 
    %>

    Much appreciated, if anyone could give me some advice.

    Comment

    • DrBunchman
      Recognized Expert Contributor
      • Jan 2008
      • 979

      #3
      Hi Karen,

      I'm not sure if this is exactly what you are looking for but you can change a date using the vbscript function DateAdd. Like so:

      <%=FormatDateTi me(DateAdd("h", 2, Now),3)%>

      The DateAdd function is explained in futher detail here

      If you are displaying a lot of dates it would probably make life easier to use a session variable in place of the number parameter. Like so:

      <%=FormatDateTi me(DateAdd("h", Session("iTimeD iff"), Now),3)%>

      I hope this helps,

      Dr B

      Comment

      • karen987
        New Member
        • Mar 2007
        • 114

        #4
        Originally posted by DrBunchman
        Hi Karen,

        I'm not sure if this is exactly what you are looking for but you can change a date using the vbscript function DateAdd. Like so:

        <%=FormatDateTi me(DateAdd("h", 2, Now),3)%>

        The DateAdd function is explained in futher detail here

        If you are displaying a lot of dates it would probably make life easier to use a session variable in place of the number parameter. Like so:

        <%=FormatDateTi me(DateAdd("h", Session("iTimeD iff"), Now),3)%>

        I hope this helps,

        Dr B
        thanks i'll try it. Thank you

        Comment

        • karen987
          New Member
          • Mar 2007
          • 114

          #5
          Sorry it didn't work, and that's because the code isn't using the config. asp file.

          Someone told me to use a global.asa file that would change the time . I'm using this, but it nothing happens, can anyone see what i need to change here? I need to move the time 2 hours forward, the hour, which means at 20pm it should show the next day's date.

          Thanks in advance.

          I'm using this code now and it doesn't change the time



          Code:
          on the global.asa page i have this only
          Code:
          <SCRIPT LANGUAGE=VBScript RUNAT=Server> 
          Public Sub Application_OnStart( ) 
                   Application("hourAdjust") = 2 
               End Sub 
          </SCRIPT>
          I even tried taking off the "public" word and it still didn't work.

          and on the page i'm using this now



          Code:
          <%=FormatDateTime( DateAdd( "h", Application("hourAdjust"), POSTED ), DATE_F )%>
          However, the web page remains unchanged, ie nothing happens.

          I have to add that the global.asa file is in the root directory. The web pages are in a directory below, so it should still work shouldn't it?


          I'm thinking I may as well just do manually on the pages, . should i use the same code as for the comments?

          thanks for all your help.

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            Can you hard code a 2 instead of using Application("ho urAdjust") - if that works then your application variable is not assigning properly.

            Give that a try and let me know if it works or not.

            Dr B

            Comment

            • karen987
              New Member
              • Mar 2007
              • 114

              #7
              im not sure what "hard code 2" means, as im totally html illiterate. I'm not even sure if i used your example above (1st one) correctly.
              Should the first code you gave have gone on the asp page itself? I presume so, and i assume i would need to change it on other pages which i have. I don't know what "session variable" is.

              Basically, my problem is that the server is in another country to the one i want to show on my news weblog. I'd like it to show 2 hours ahead, as that's what the correct time is, and i'd also like to know which part i would need to alter, (add or subtract) during daylight saving. I assume it's the part which is "2"?

              Thanks a bunch

              Comment

              • DrBunchman
                Recognized Expert Contributor
                • Jan 2008
                • 979

                #8
                Sorry Karen, I'll try and be clearer.

                If you just put the following on your asp page does it show the time you want?

                <%=FormatDateTi me(DateAdd("h", 2, Now),3)%>

                This function is getting the current time (that's the Now part) and adding 2 to the number of hours (that's the DateAdd("h", 2, Now) bit).

                Does this display the correct time for you?


                Dr B

                Comment

                • karen987
                  New Member
                  • Mar 2007
                  • 114

                  #9
                  Yes the code above does show the time it shows like this, and it moved it 2 hours forward,

                  10:12:00 PM when the time was 8.12pm here.

                  so yes, it works. But actually that's not what i wanted,

                  Sorry i didn't explain it right, or maybe you misunderstood. Let me briefly sum it up.

                  I have this code
                  Code:
                  <%= FormatDateTime(POSTED,DATE_F) %>
                  which shows as

                  3/21/2008


                  At midnight GMT it automatically goes to 3/29/2008 therefore any articles added after midnight show the next day. The server is another country whose time is GMT +2, to the one the news weblog is in, so I want to add 2 hours, so that instead of adding material at midnight GMT , i can add it at 10pm here, which if the code is altered will show as the next day. I hope you understand what i'm trying to do. Basically, the server is another country to the one it represents.

                  The code that you gave me to alter time is this
                  Code:
                   <%=FormatDateTime(DateAdd("h", 2, Now),3)%>
                  and it shows the time, like this:
                  10:12:00 PM
                  what i want is for it to continue to show the date as above, but just to be able to move forward, so that at 10pm GMT it will be midnight, ie. the date will show the next day.


                  So do you see what the problem is? The code you gave me literally shows the hours. However what i want to do is to add 2 hours to the date.

                  can you suggest an alternative code that can do what i want? Much appreciated, Thank You.

                  Comment

                  • DrBunchman
                    Recognized Expert Contributor
                    • Jan 2008
                    • 979

                    #10
                    Hi Karen, using the DateAdd function will move the date on - the date wasn't showing because of the formatting used.

                    If you use
                    Code:
                     Response.Write(DateAdd("h", 2, "27/03/08 23:00"))
                    you'll see that 2 hours have been added to the time and the date has ticked over to the next day. Can you make any use of this?

                    Sorry if I've misunderstood you again!

                    Dr B

                    Comment

                    Working...