Abbreviating the year in ASP - asp-classic

I am working on a calendar application and I want to display the date such as: mm/dd/yy. The reason for this is because on mobile devices some of my dates are getting recognized as phone numbers when it is in mm/dd/yyyy. I couldn't find a VBScript function to accomplish this so I tried it with the following code:
listyear = Year(strlistdate)
listyearabbr = Right(listyear, 2)
strlistdate = Replace(strlistdate, listyear, listyearabbr)
Where strlistdate is the initial date returned from the database. I then display the date using Response.write("<td>" &FormatDateTime(strlistdate,2)&"</td>")
This didn't work and I was wondering if someone could give me a few pointers on how to achieve this.
Thanks

I don't think that this is a good approach, because you'll end up returning the same date format for all locales, but you could do this:
response.write(Month(strlistdate) & "/" & Day(strlistdate) & "/" & Right(Year(strlistdate),2))
Anytime you use FormatDateTime, it will create a year based on the definition stored on the server. If the server can be set to mm/dd/yy, then you can get the output you want without doing any of the above.
Also, look into the format function. You should be able to do this:
response.write(Format(strlistdate, "m/dd/y")

You could try this idea. I haven't used date formatting for years. Instead I construct the date field like so...
strDay = Day(Date)
strMonth = Month(Date)
strYear = Year(Date)
strHours = Hour(Now)
strMins = Minute(Now)
strSecs = Second(Now())
if len(strMonth) = 1 then
strMonth = "0" & strMonth
end if
if len(strDay) = 1 then
strDay = "0" & strDay
end if
if len(strHours) = 1 then
strHours = "0" & strHours
end if
if len(strMins) = 1 then
strMins = "0" & strMins
end if
if len(strSecs) = 1 then
strSecs = "0" & strSecs
end if
strDateAdded = strYear & "-" & strMonth & "-" & strDay
strDateAddedTime = strDateAdded & " " & strHours & ":" & strMins
Using this method you have complete control over the order and even when running your web app in different time zones, you still maintain DD/MM format... or whatever order you want such as MM-DD-YY (by reordering and trimming the year). Personally I prefer YYYY-MM-DD because sorting by ASC and DESC is a lot easier to work with, ie: easier to read because all rows will have the same number of characters like:
2013-04-01 03:15
2013-04-09 10:15
2013-04-22 07:15
2013-04-23 10:15
2013-04-23 10:60
2013-10-25 12:01
2013-10-25 12:59
Instead of:
2013-4-1 3:15
2013-4-9 10:15
2013-4-22 7:15
2013-4-23 10:15
2013-4-23 10:60
2013-10-25 12:1
2013-1-25 12:59

Related

How to convert one date/time format to another from querystring?

I'm getting "August 22, 2015 10:55" in a querystring and I want to convert it to "2015-08-22 10:55".
I have this so far:
datum=request.querystring("tid")
response.write datum ----this writes out August 22, 2015 10:55
datum=Cstr(datum)
DateVar = datevalue(datum)
NewDateVar = year(DateVar) & "-" & month(DateVar) & "-" & day(DateVar)
NewTimeVar=timevalue(datum)
newdatum = NewDateVar & " " & NewTimeVar
But now I get a type missmatch error on DateVar = datevalue(datum).
If I use datum="August 22, 2015 10:55" to test with, then it works.
To spell it out:
>> s = "August 22, 2015 10:55"
>> d = CDate(s)
>> WScript.Echo TypeName(d), d
>>
Date 22.08.2015 10:55:00 <-- german locale!
Added:
To get the required format, you can use the strategy described here.
DateVar = datevalue("August 22, 2015 10:55")
NewDateVar = year(DateVar) & "-" & month(DateVar) & "-" & day(DateVar) & "-" & time(DateVar)
Classic ASP tends to use US date formats, come hell or high water. However, it's easy enough to format the date you display (or pass on to the database) in whatever way you wish.
The following will pad any single-digit values with a 0. If you don't need that, then just use the plain functions, without Right().
Dim datum, mm, dd, yyyy, hh, nn, fdatum
datum = Request.Querystring("tid")
If IsDate(datum) Then
datum = CDate(datum)
mm = Right(100 + Month(datum),2)
dd = Right(100 + Day(datum),2)
yyyy = Year(datum)
hh = Right(100 + Hour(datum),2)
nn = Right(100 + Minute(datum),2)
fdatum = yyyy & "-" & mm & "-" & dd & " " & hh & ":" & nn
Else
fdatum = datum '- or whatever fallback value you want
End If

correct sum of hours in access

I have two columns in an access 2010 database with some calculated field:
time_from time_until calculated_field(time_until-time_from)
10:45 15:00 4:15
13:15 16:00 2:45
11:10 16:00 4:50
08:00 15:00 7:00
08:00 23:00 15:00
Now so far, it is good: calculated field did its job to tell me total hours and mins...
now, I need a sum of a calculated field....
I put in an expression builder: =Sum([time_until]-[time_from])
I guess total sum should give me 33:50... but it gives me some 9:50. why is this happening? Is there a way to fix this?
update:
when I put like this:
=Format(Sum([vrijeme_do]-[vrijeme_od])*24)
I get a decimal point number... which I suppose is correct....
for example, 25hrs and 30mins is shown as 25,5
but, how do I format this 25,5 to look like 25:30?
As #Arvo mentioned in his comment, this is a formatting problem. Your expected result for the sum of calculated_field is 33:50. However that sum is a Date/Time value, and since the number of hours is greater than 24, the day portion of the Date/Time is advanced by 1 and the remainder 9:50 is displayed as the time. Apparently your total is formatted to display only the time portion; the day portion is not displayed.
But the actual Date/Time value for the sum of calculated_field is #12/31/1899 09:50#. You can use a custom function to display that value in your desired format:
? duration_hhnn(#12/31/1899 09:50#)
33:50
This is the function:
Public Function duration_hhnn(ByVal pInput As Date) As String
Dim lngDays As Long
Dim lngMinutes As Long
Dim lngHours As Long
Dim strReturn As String
lngDays = Int(pInput)
lngHours = Hour(pInput)
lngMinutes = Minute(pInput)
lngHours = lngHours + (lngDays * 24)
strReturn = lngHours & ":" & Format(lngMinutes, "00")
duration_hhnn = strReturn
End Function
Note the function returns a string value so you can't do further date arithmetic on it directly.
Similar to the answer from #HansUp, it can be done without VBA code like so
Format(24 * Int(SUM(elapsed_time)) + Hour(SUM(elapsed_time)), "0") & ":" & Format(SUM(elapsed_time), "Nn")
I guess you are trying to show the total in a text box? the correct expression would be =SUM([calculated_field_name]).

IsDate validates wrong format

I have these lines
SetLocale(3081)
Response.Write "<p>TEST: " & Date() & " | " & isDate("3/22/2014") & " --> " & GetLocale() & "</p>"
which outputs
TEST: 3/07/2014 | True --> 3081
now correct me if i'm wrong but isn't there only 12 months in a year?, according to IsDate the date i've passed, which should be wrong because i have put in 22 as the month, is valid despite the local settings saying otherwise.
i want to validate the date to be the correct format to insert into the database and if it isn't give a more friendly error, "3/22/2014" will output "Error converting data type varchar to date." when i try to inster it into the database because it's getting by the IsDate check
What have i done wrong here?
Yes, this is indeed a valid date.
Why? Because VBScript is smart/generous/stupid (choose your favorite) enough to treat numbers as dates. And "3/22/2014" can be parsed as formula: 3 / 22 / 2014 = 6.770786313983931e-5
Now take this number and convert to date:
Dim myNumber, myDate
myNumber = 6.770786313983931e-5
myDate = CDate(myNumber)
The variable myDate will be perfectly valid date, which is December 30th 1899, 00:00:06
So bottom line: the value is a date, just not what you expect. You've done nothing wrong, but to really check if a string is valid date you will have to check it yourself, no out of the box methods.

VBScript - Date and Time Constraints for Running a File

Ok I am new with writing VBScript and I want to write a string of code that plays a file (WAV format) only on a certain day and only between specific times. After piecing together multiple fragments of code I found on the internet I was left with the following:
Dim myDateString
Dim thing1
thing1 = 0
myDateString = Date()
If myDateString < "13/08/13" Then
thing1 = 1
end if
if thing1 = 1 then
If myDateString > "15/08/13" Then
thing1 = 2
end if
end if
if thing1 = 2 then
hournow = hour(Time())
If hour(Time()) >= 9 And Hour(Now()) < 22 Then
set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
WshShell.Run "wmplayer """ & music & """",0,True
Else
wscript.quit 1
End If
Else
wscript.quit 1
End If
Ok so I had set this for the date I ran this on, within the hour I was in. But
it didn't work. I expected the VBS to start playing MYSOUND.wav but it didn't. When running the file
there were no errors though, so I was wondering what I did wrong!
I running Windows 7
If anyone could tell me what I did wrong, and how to fix it that would be great.
Double points if anyone could post a corrected version of the code!
Thanks to any answers!
First, indent your code and give your variables meaningful names!
Then, your date comparison doesn't work because you're trying to compare strings as if they were dates. This usually won't work (depending on your "system locale"): you need to use date type variables and an actual date comparison function (DateDiff in VBScript).
(EDIT: as Ansgar Wiechers pointed out, you don't need to use DateDiff to compare dates in VBScript, "DateStart <= Now And Now <= DateEnd" will do just fine)
Try this:
Dim DateStart, DateEnd, WshShell, music
DateStart = DateSerial(2013, 8, 13)
DateEnd = DateSerial(2013, 8, 15)
If DateDiff("D", DateStart, Now) >= 0 And DateDiff("D", Now, DateEnd) >= 0 Then
If Hour(Now) >= 9 And Hour(Now) < 22 Then
'*** delete after debugging ***
MsgBox "play sound"
Set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
'*** 2nd parameter : 0 hides wmplayer, 1 shows it ***
WshShell.Run "wmplayer """ & music & """", 1, True
Else
'*** delete after debugging ***
MsgBox "Not the right time"
End If
Else
'*** delete after debugging ***
MsgBox "Not the right day"
End If
Also, if you want to debug a small script like this, you can call MsgBox to do a simple tracking of what's actually executed (in your example, replacing your "WScript.Quit 1" by MsgBox would show you that the date is not properly compared.

ASP formatting date

Hello there I'm trying to get a date in ASP to show up in a particular format (yyyymmdd). This is what I've tried so far but no luck. Any help is appreciated. Thanks
<tr>
<td><b>Call Date</b></td>
<% for i = -6 to 0 %>
<td align=center>
X
</td>
<% Next %>
</tr>
You can make use of the following functions:
Year(Now) '' Year in 4 digits
Month(Now) '' Month without leading zero
Day(Now) '' Day of the month without leading zero
DateAdd("d", <numofdays>, Now) '' add a number of days to your date
Read more about these (and other date functions) functions here.
If you need to add a leading zero:
function addLeadingZero(value)
addLeadingZero = value
if value < 10 then
addLeadingZero = "0" & value
end if
end function
An example of your case would be:
Dim today, myDate
today = Now
for i = -6 to 0
myDate = DateAdd("d", i, today)
response.write "X"
next
Sorry to dig this up, but it might be of help to some people. Rather than the "If<10 then add leading zero" logic, I often use the right command and always add a leading zero...
response.write "X"
..This way, you don't need a separate function, and it can be done on one line. I can't speak for the efficiency of it, but it seems logical.
ASP gets the date from the OS not from the Database, a common error, but it is solved by use:
<%
' Date dd/mm/yyyy
Session.lcid=2057 '= UK English
%>
I hope it helps people.
You can try. 100% tested!
<%
mm = Month(now())
dd = Day(now())
yy = Year(now())
IF len(mm) = 1 THEN
mm = "0" & mm
END IF
IF len(dd) = 1 THEN
dd = "0" & dd
END IF
response.write(yy & "/" & mm & "/" & dd)
%>
<%= DatePart("yyyy", Now) & "/" & DatePart("m", Now) & "/" & DatePart("d", Now) %>
Also refer
http://www.w3schools.com/vbscript/vbscript_ref_functions.asp
http://www.mikesdotnetting.com/Article/22/Date-formatting-in-VBScript
Thanks
Deepu
Mid(date(), 7,4) & "-" & Mid(date(), 4,2) & "-" & Left(date(), 2)

Resources