Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old March 14th, 2008, 12:28 AM
Member
 
Join Date: Mar 2007
Posts: 114
Default 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:
Expand|Select|Wrap|Line Numbers
  1. <%=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

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




Expand|Select|Wrap|Line Numbers
  1.   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" 
  2.                             Set RS = Server.CreateObject("ADODB.Recordset") 
  3.                             RS.LockType   = 1 
  4.                             RS.CursorType = 0 
  5.                             RS.Open SQL, MyConn     
  6.                                  WHILE NOT RS.EOF 
  7.                                     CCOUNT    = CCOUNT + 1 
  8.                                       C_ID      = trim(RS("ID")) 
  9.  
  10.                                     C_NAME    = trim(RS("fldNAME")) 
  11.                                     C_COMMENT = trim(RS("fldCOMMENT")) 
  12.                                  C_SUBJECT = trim(RS("fldSUBJECT")) 
  13.                                  C_M_ID    = trim(RS("fldM_ID")) 
  14.                                     C_DATE    = trim(RS("fldDATE")) 
  15.                                  C_CITY    = trim(RS("fldCITY")) 
  16.                                  C_COUNTRY = trim(RS("fldCOUNTRY")) 
  17.                                  C_ALLOW_E = trim(RS("fldALLOW"))     
  18.                                  C_EMAIL = trim(RS("fldEMAIL"))     
  19.  
  20.                                     %>     
  21.  
  22.  
  23.  
  24.  
  25.  
  26.                                     <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;"> 
  27.                                     <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> 
Reply
  #2  
Old March 16th, 2008, 10:09 AM
Member
 
Join Date: Mar 2007
Posts: 114
Default

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"


Expand|Select|Wrap|Line Numbers
  1. <%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 
  2.  
  3.     CID     = Trim(Request.QueryString("CID")) 
  4.     CNAME = Request("CNAME") 
  5.     iStart  = Request.QueryString("Start") 
  6.     iOffset = Request.QueryString("Offset") 
  7.  
  8.  
  9.     IF IS_VALID_ID(CID) THEN 
  10.  
  11.         If Not IsNumeric(iStart) or Len(iStart) = 0 then 
  12.           iStart = 0 
  13.         Else 
  14.           iStart = CInt(iStart) 
  15.         End If 
  16.         If Not IsNumeric(iOffset) or Len(iOffset) = 0 then 
  17.           iOffset = ALL_ARTICLES_PAGE_SIZE 
  18.         Else 
  19.           iOffset = Cint(iOffset) 
  20.         End If     
  21.  
  22.         IF DB_TO_USE = 1 OR DB_TO_USE = 3 THEN     
  23.             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" 
  24.         ELSE 
  25.             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" 
  26.         END IF 
  27.  
  28.         Call OPEN_DB() 
  29.  
  30.         Set RS = MyConn.Execute(SQL) 
  31.         IF NOT RS.EOF THEN 
  32.             strRETURNED_DATA = RS.getrows 
  33.             EOF_VAL = False 
  34.         ELSE 
  35.             EOF_VAL = True     
  36.         END IF     
  37.         RS.close 
  38.         Set RS = Nothing 
  39.         ' Get settings 
  40.         Dim DATE_F 
  41.         DATE_F = GET_SETTINGS(False, "fldDATE_F") 
  42.  
  43.  
  44.         IF not EOF_VAL = True THEN 
  45.             iRows = UBound(strRETURNED_DATA, 2) 
  46.             iCols = UBound(strRETURNED_DATA, 1) 
  47.             If iRows > (iOffset + iStart) Then 
  48.                iStop = iOffset + iStart - 1 
  49.             Else 
  50.                iStop = iRows 
  51.             End If             
  52.             FOR iRowLoop = iStart to iStop 
  53.                 NID = strRETURNED_DATA(0, iRowLoop) 
  54.                 SUMMARY = strRETURNED_DATA(1, iRowLoop) 
  55.                 IMAGE = strRETURNED_DATA(2, iRowLoop) 
  56.                 TITLE = strRETURNED_DATA(3, iRowLoop) 
  57.                 AUTHOR = strRETURNED_DATA(4, iRowLoop) 
  58.                 POSTED = strRETURNED_DATA(5, iRowLoop) 
  59.                 TITLE = PROCESS_SHORTCUTS(False, TITLE) 
  60.                 SUMMARY = PROCESS_SHORTCUTS(False, SUMMARY)     
  61.                 CATEGORIES = GET_CATES(NID)         
  62.                             %> 
  63.  
  64.  
  65.                   <table cellpadding="2" cellspacing="0" border="0" width="100%"><tr> 
  66.                       <td><div align="justify"><a href="page.asp?ID=<%=NID%>" class="aTITLE"><%= TITLE %></a></div></td> 
  67.                 </tr> 
  68.                 <tr><td colspan="2" class="divPOSTEDON"><div align="justify"><%= FormatDateTime(POSTED,DATE_F) %> 
  69.                   <% IF SHOW_AUTHOR = True THEN %> 
  70.    By:<%= AUTHOR %> 
  71. <% Else %> 
  72.  
  73. <% End If %> 
  74.                 </div></td></tr> 
  75.                <tr><td colspan="2" class="tdSUMMARY"><div align="justify"> 
  76.                     <%IF NOT (IMAGE = "" OR IsNull(IMAGE)) THEN%> 
  77.                       <img src="<%=IMAGE%>" width="50" height="50" border="1" align="left" /> 
  78.                   <% END IF %> 
  79.                   <%= SUMMARY %></div></td></tr> 
  80.                 </table> 
  81.                  <div align="justify"><hr width="100%" size="1" style="color: gray;height: 1px;width: 100%;" /> 
  82.  
  83.                       <% 
  84.             NEXT 
  85.             ' Close DB         
  86.             MyConn.close             
  87.             Set MyConn = Nothing         
  88.  
  89.             If iStart > 0 Then 
  90.              strPREV_LINK = "<A class=""clsPAGING"" HREF=""politics.asp?Start=" & iStart-iOffset & "&Offset=" & iOffset & "&CID=" & CID & """>Previous " & iOffset & "</A>" 
  91.             Else 
  92.                 strPREV_LINK = " " 
  93.             End If     
  94.             If iStop < iRows Then 
  95.              strNEXT_LINK = " <A class=""clsPAGING"" HREF=""politics.asp?Start=" & iStart+iOffset & "&Offset=" & iOffset & "&CID=" & CID & """>Next " & iOffset & "</A>" 
  96.             Else         
  97.              strNEXT_LINK = " "     
  98.             End If %>                     
  99.  
  100.             <table width="100%" align="center" cellpadding="2" cellspacing="0" border="0"><tr class="trPAGING"> 
  101.              <td width="50%" align="left"><%=strPREV_LINK%></td> 
  102.              <td width="50%" align="right"><%=strNEXT_LINK%></td> 
  103.             </tr></table>     
  104.  
  105.         <%ELSE%>     
  106.  
  107.                 <table width="100%" align="center" cellpadding="2" cellspacing="0" border="0"><tr><td>No political news yet</td></tr></table> 
  108.  
  109.         <%END IF%>         
  110.     <%END IF%> 
  111.  

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


Expand|Select|Wrap|Line Numbers
  1.    ' Assemble date value 
  2.     FUNCTION ASEMBLE_DATE_FORMAT(sDAY, sMONTH, sYEAR) 
  3.         Dim DATE_FORMAT, sDATE                   
  4.  
  5.         DATE_FORMAT = 1 ' US Locale 
  6.  
  7.         SELECT CASE DATE_FORMAT 
  8.             CASE 1 
  9.                 sDATE = sMONTH & "/" & sDAY  & "/" & sYEAR 
  10.             CASE ELSE 
  11.                 sDATE = sMONTH & "/" & sDAY  & "/" & sYEAR 
  12.         END SELECT             
  13.         ASEMBLE_DATE_FORMAT = sDATE 
  14.     END FUNCTION 
  15.  
  16.  
  17.  
  18.     END SUB               
  19. %> 
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


Expand|Select|Wrap|Line Numbers
  1. <% '// NO NEED TO MODIFY ANYTHING BELOW 
  2. FUNCTION CNT_DATE(sDATE) 
  3.   IF IsDate(sDATE) = True THEN 
  4.     SELECT CASE DATE_FORMAT 
  5.         CASE 1        'YYYYMMDD 
  6.             CNT_DATE = Year(sDATE) & Right(Cstr(Month(sDATE) + 100),2) & Right(Cstr(Day(sDATE) + 100),2) 
  7.         CASE 2        'YYYY-MM-DD 
  8.             CNT_DATE = Year(sDATE) & "-" & Right(Cstr(Month(sDATE) + 100),2) & "-" & Right(Cstr(Day(sDATE) + 100),2) 
  9.         CASE 101    'mm/dd/yy 
  10.             CNT_DATE = Right(Cstr(Month(sDATE) + 100),2) & "/" & Right(Cstr(Day(sDATE) + 100),2) & "/" & Right(Cstr(Year(sDATE)),2) 
  11.         CASE 102    'yy.mm.dd 
  12.             CNT_DATE = Right(Cstr(Year(sDATE)),2) & "." & Right(Cstr(Month(sDATE) + 100),2) & "." & Right(Cstr(Day(sDATE) + 100),2) 
  13.         CASE 103    'dd/mm/yy 
  14.             CNT_DATE = Right(Cstr(Day(sDATE) + 100),2) & "/" & Right(Cstr(Month(sDATE) + 100),2) & "/" & Right(Cstr(Year(sDATE)),2) 
  15.         CASE 104    'dd.mm.yy 
  16.             CNT_DATE = Right(Cstr(Day(sDATE) + 100),2) & "." & Right(Cstr(Month(sDATE) + 100),2) & "." & Right(Cstr(Year(sDATE)),2) 
  17.         CASE 105    'dd-mm-yy 
  18.             CNT_DATE = Right(Cstr(Day(sDATE) + 100),2) & "-" & Right(Cstr(Month(sDATE) + 100),2) & "-" & Right(Cstr(Year(sDATE)),2) 
  19.         CASE 110    'mm-dd-yy 
  20.             CNT_DATE = Right(Cstr(Month(sDATE) + 100),2) & "-" & Right(Cstr(Day(sDATE) + 100),2) & "-" & Right(Cstr(Year(sDATE)),2) 
  21.         CASE 111    'yy/mm/dd 
  22.             CNT_DATE = Right(Cstr(Year(sDATE)),2) & "/" & Right(Cstr(Month(sDATE) + 100),2) & "/" & Right(Cstr(Day(sDATE) + 100),2)     
  23.         CASE 112    'yymmdd 
  24.             CNT_DATE = Right(Cstr(Year(sDATE)),2) & Right(Cstr(Month(sDATE) + 100),2) & Right(Cstr(Day(sDATE) + 100),2) 
  25.     END SELECT     
  26.   ELSE 
  27.       CNT_DATE = Null 
  28.   END IF 
  29. END FUNCTION 
  30. %> 

Much appreciated, if anyone could give me some advice.
Reply
  #3  
Old March 17th, 2008, 11:49 AM
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 924
Default

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:

<%=FormatDateTime(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:

<%=FormatDateTime(DateAdd("h", Session("iTimeDiff"), Now),3)%>

I hope this helps,

Dr B
Reply
  #4  
Old March 18th, 2008, 08:48 PM
Member
 
Join Date: Mar 2007
Posts: 114
Default

Quote:
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:

<%=FormatDateTime(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:

<%=FormatDateTime(DateAdd("h", Session("iTimeDiff"), Now),3)%>

I hope this helps,

Dr B
thanks i'll try it. Thank you
Reply
  #5  
Old March 19th, 2008, 07:38 AM
Member
 
Join Date: Mar 2007
Posts: 114
Default

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



Expand|Select|Wrap|Line Numbers
  1. on the global.asa page i have this only
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE=VBScript RUNAT=Server> 
  2. Public Sub Application_OnStart( ) 
  3.          Application("hourAdjust") = 2 
  4.      End Sub 
  5. </SCRIPT> 
I even tried taking off the "public" word and it still didn't work.

and on the page i'm using this now



Expand|Select|Wrap|Line Numbers
  1. <%=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.
Reply
  #6  
Old March 19th, 2008, 10:13 AM
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 924
Default

Can you hard code a 2 instead of using Application("hourAdjust") - 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
Reply
  #7  
Old March 19th, 2008, 07:19 PM
Member
 
Join Date: Mar 2007
Posts: 114
Default

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
Reply
  #8  
Old March 20th, 2008, 09:53 AM
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 924
Default

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?

<%=FormatDateTime(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
Reply
  #9  
Old March 21st, 2008, 09:21 PM
Member
 
Join Date: Mar 2007
Posts: 114
Default

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
Expand|Select|Wrap|Line Numbers
  1. <%= 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
Expand|Select|Wrap|Line Numbers
  1.  <%=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.
Reply
  #10  
Old March 27th, 2008, 01:12 PM
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 924
Default

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

If you use
Expand|Select|Wrap|Line Numbers
  1.  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
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles