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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't extract Year, Month, Day out of a date 1

Status
Not open for further replies.

bzeng

Programmer
Dec 11, 2003
13
JP
Hi, due to the different local setting (Date setting) around the world I have to manually create a standard date format for our online database system.(ASP) We prefer to use YYYY/MM/DD. But the hosting server is MM/DD/YYYY.

It works fine if I just want to print out the date

<td><%=ProjectDate%> </td>

but when I tried to do
<td><%=year(ProjectDate)&&quot;/&quot;&month(projectdate)&&quot;/&quot;&day(projectdate)%> </td>

it gives me the following error message

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch
/ZAO/App/Project/Invoice.asp, line 204


However I can do the following script without a problem

today=year(date())&&quot;/&quot;&month(date())&&quot;/&quot;&day(date())


I have no idea what I did wrong. I have even tried to use Cdate().

the following code works fine

<td><%=Cdate(ProjectDate)%> </td>

however when I add year(), it gives me the same error. (I test it with Year() only this time)

<td><%=Year(Cdate(ProjectDate))%> </td>


What's wrong with my code? (I think this should be a very simple command) or is there a better way to use a standard date format without doing all these complicated manual format.


Thanks in advance
 
Hello bzeng,

Use CDate(ProjectDate) instead? Again, there is a localization element to consider as well. This is what documentation said &quot;In general, hard coding dates and times as strings is not recommended.&quot;

regards - tsuji
 
I suspect the problem is that you are trying to use numbers as strings and running into type mismatches. People can get away with a lot in VBScript, because in a lot of cases type conversions can be figured out by the script engine within expressions.

Another sloppy thing people do though is leave out spaces in expressions: VBScript really wants to see spaces around operators. Sometimes I get the idea that people try to write VBScript without learning VB first. The VB IDE really makes this sort of error harder by correcting your mis-spaced syntax for you.

The abuse of the & in string expressions can really bite you here. The & has more than one possible meaning in VBScript, and if you don't assist the parser with clean syntax it can make the wrong guess as to your intent. Remember, &quot;&H&quot; is the prefix indicating a hexadecimal literal and &quot;&O&quot; is for octal literals. Therefore ramming & up against a letter is just asking for trouble.


Sadly, I'm not aware of any convenient built-in VBScript function or generally available ActiveX component that exposes a date formatting method with great flexibility. In a case like this I'd probably just roll my own, such as:
Code:
Function FmtYMD(ByVal dtVal)
  FmtYMD = CStr(Year(dtVal)) & &quot;/&quot; _
         & Mid(CStr(Month(dtVal) + 100), 2) & &quot;/&quot; _
         & Mid(CStr(Day(dtVal) + 100), 2)
End Function

:

<td><%=FmtYMD(ProjectDate)%></td>
I haven't tested this. I just scribbled it inline here, so there may be a goof or two there but you get the idea.

To get the leading zeros on the month and day I use a cheap trick of adding 100 and then after converting to a string I grab the value starting at the 2nd character. You could do this sort of thing with the year too if you wanted leading zeros for year values prior to the year 1000 AD I suppose.

;-)
 
bzeng,

I needed to get 2 digit dates and this is what I did. You can shorten it by following the process I used for just the year.

---<snip start>---

TodaysDate = Date

if month(TodaysDate) < 10 then
SMonth = &quot;0&quot; & month(TodaysDate)
else
SMonth = month(TodaysDate)
end if

if day(TodaysDate) < 10 then
SDay = &quot;0&quot; & day(TodaysDate)
else
SDay = day(TodaysDate)
end if

SYear = right(year(TodaysDate),2)

---<snip end>---

Hope this helps. :)
 
bzeng,

Sorry, I didn't help. I hadn't read through and you did try cdate()!

In that case, I would response.write the thing to see what the variable really looks like.

- tsuji
 
Another good point tsuji. I assumed that
Code:
ProjectDate
is actually a date when it might well be a string here holding a date value of some sort. Or maybe even not a date.

Using something like the
Code:
Month()
function against a string that won't coerce into a date will lead to an error as well. For example if this is user input it should have been edited before getting this far, but we don't really know where it came from.
 
Hi, Thanks all for your reply.

I guess I need to provide more info regarding to my question.

1. The database is MS Access and the field where projectDate comes from is defined as &quot;Date/Time&quot;. Therefore projectDate is not a string or number or anything else. It is defined in Access with datatype=&quot;Date/Time&quot;.

2. Instead of assign the open recordset to a variable named ProjectDate, I replace it with <%rst(&quot;startdate&quot;)%>

<td><%=month(rst(&quot;startdate&quot;))%> </td>

the problem is that I kept getting error even I do this. That means it won't work for those code you made. I couldn't even get the month out of the date. (either can I get Year or Day)

I wonder what's wrong with <%rst(&quot;startdate&quot;)%>. I can print the date like this
<td><%=rst(&quot;startdate&quot;)%></td>

but the date is 1/9/2004 for example. I want it to be print as 2004/1/9.


thanks

Brian
 
Hello again,

I believe things still follow their impeccable logic. Let have full control over the server side before filling up the table? Take startdate as an example. Something like...
Code:
<%
    '... the other stuff
    Dim isoStartDate
    isoStartDate=rst(&quot;startdate&quot;)
    isoStartDate=split(isoStartDate,&quot;/&quot;)(0) & &quot;/&quot; & _
        split(isoStartDate,&quot;/&quot;)(1) & &quot;/&quot; & _
        split(isoStartDate,&quot;/&quot;)(2)
    '... the other stuff
%>

    <td><%=isoStartDate%></td>
See what gives?

regards - tsuji
 
Hi, tsuji:
Thanks very much!!

projectDate=split(projectDate,&quot;/&quot;)(2) & &quot;/&quot; & _
split(projectDate,&quot;/&quot;)(0) & &quot;/&quot; & _
split(projectDate,&quot;/&quot;)(1)

although I think there should be a simple command to do that. Like formatDate(projectdate, &quot;YYYY/MM/DD&quot;)

If the system move to a different server with different local setting, I have to update the order again...

Anyway, thank you all.

regards
 
bzeng, why not amend your select statement to return Year(startdate), Month(startdate) and Day(startdate) ?

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top