Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing my own DatePart() function 1

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
GB
sorry about the multipost but I don't know how to generate a link to the other thread.

I want to get the week number of the date entered. In Access I can use
DatePart("ww", DateEntered)
to return the week number (say 43 or whatever).

However I'm coding a set of active server pages for my front end and I must find the week number for any given date. Does anyone know how to get this without having a HUGE number of select case statements with each day of the year?
 
use the same code....datePart("ww",yourDate)
 
I tried
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
DatePart(&quot;ww&quot;, NOW())
</SCRIPT>

but nothing is displayed. what have I done wrong?
 
If you are using it client side then:

<SCRIPT LANGUAGE=&quot;VBScript&quot;>
alert(DatePart(&quot;ww&quot;, NOW()))
</SCRIPT>


server side:

<% document.write DatePart(&quot;ww&quot;, NOW())%>


 
About the server side...

I got the following error

Microsoft VBScript runtime error '800a01a8'

Object required: ''


I've put the code in the BODY that's fine isn't it?

I don't know why <% document.write DatePart(&quot;ww&quot;, NOW())%> won't work, the document is obviously the webpage.
 
OOPS!

<%response.write DatePart(&quot;ww&quot;, NOW())%>

document.write is javascript....sorry.....
 
Ah well now I have both - thanks!

One quick point, can I use javascript and VBscript in the same .asp page?

e.g.

<SCRIPT LANGUAGE=&quot;VBScript&quot;>
Sub SetDateButton_OnClick

Dim UsersDateChoice

'Assign the user's date to the appropriate variable
UsersDateChoice = document.calendarForm.calendarField

u_fname = UsersDateChoice

End Sub

</SCRIPT>


<SCRIPT language=Javascript>

function show(id){ Div(id).show() }
function hide(id){ Div(id).hide() }
function displayAsBlock(id){ Div(id).setAsBlock() }
function displayAsNone(id){ Div(id).setAsNone() }

</SCRIPT>

// do stuff in the body...
 
Yes, but I recommend that you put you javascript tags in first (opposite of what you've done). Otherwise, you cannot use the 'this' object that javascript provides (which is very handy). Also, be careful not to use the same variable names in both script - you can get weird results.... Also, remember that client side vbscript doesn't work in Netscape(I've heard that it might in NS6, but I don't know for sure).
 
Excellent - but there's more...

How can I set this to dynamically update when the user types in a date in to the date field?

It's a tricky one...
 
You need to trigger a function when the users changes the date. So maybe you could use an onChange event in the textbox tag of your html.

<input type=&quot;text&quot; onChange=&quot;somefunction&quot;

In the function (vbscript or javascript), calculate your datepart and write it down somewhere. you could use another textbox for that (if you want, make the textbox style so that it doens't look like a textbox, border:0px etc.)
...

just an idea
 
Good thought, though I'm stuck

Date
<input type=&quot;text&quot; name=&quot;DateField&quot; size=&quot;20&quot; value=&quot;<%response.write Date()%>&quot; onchange=&quot;RedoWeek(value)&quot; >


How do I pass the current value of the text box as a parameter?

then

How can I assign the value of my calculation to the value of the text box?


RedoWeek(PassedDate)
{
TheWeekVariable = DatePart(&quot;ww&quot;, PassedDate)
// now what?
}
 
Can anyone help with dynamically updating the week number?
 
Is this what you need?

Code:
<input type=&quot;text&quot; name=&quot;DateField&quot; size=&quot;20&quot; value=&quot;<%response.write Date()%>&quot; onBLUR=&quot;RedoWeek(this)&quot; >
<input type=&quot;hidden&quot; name=&quot;weekNum&quot; value=&quot;<%=DatePart(&quot;ww&quot;, DATE())%>&quot; >

<script language=vbs>
  sub RedoWeek(inDate)
    if isDate(inDate.value) then
      document.myForm.weekNum.value = DatePart(&quot;ww&quot;, inDate.value())
    else
      inDate.value = date()
    end if
  end sub
</script>
-- What did you expect? This is FREE advice. LOL[ponder]
 
bah, I keep getting
&quot;object does not support this action: indate.value&quot;

I'm trying to fiddle with it

 
GOT IT!!!


<!-- RedoWeek() dynamically updates the week field depending on what the date field is set to.

-->
<script language=VBSCRIPT>
sub RedoWeek(inDate)
if isDate(inDate.value) then
document.SubmitButtonForm.WeekField.value = DatePart(&quot;ww&quot;, inDate.value)
else
inDate.value = date()
end if
end sub
</script>



indate.value() is what caused it - value is not a function.

Many thanks!

Hey somebody star me for once huh?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top