How do I get a time stamp with ASP Classic from code? - asp-classic

I am using:
<%= time %>
It's returning:
5:12:19
And it's 8:12:19 p.m. where I am
How can I make it print the local time?

You have to set the LocaleId (Session.LCID or SetLocale(lcid)) for your own timezone before calling time. See more about SetLocale and LocaleIDs on this page

<% myDateTime = DateAdd("h", 3, Time) %>

Related

ASP, Forms and passing variables between frames

I am pretty new to ASP, I know VBScript reasonably well though. What I am trying to do is create a website with 2 frames. In the top frame, it asks for a year (from a selection box) and a week number (from a selection box). It should then display the dates relating to the selection and a button to process the request. When the button is clicked the bottom form then processes a SQL query based on the selection in the top frame and displays the info.
Now, my problem is when it comes to understanding ASP. With ASP, all the code is processed then the output is sent to the browser. How do you update variables or even pass them to other frames when the code has already processed?
I just need some pointers on the way forward to accomplishing the above.
Thanks
First off, don't use frames: they're annoying, ugly, and outmoded.
You can do something like this in asp, but it's going to require a round trip (or two) to the server.
The basic outline of the page (let's call it thispage.asp) would be something like
<html><head>[head stuff]
<%
dim yr, wk, i
yr = request.form("Year")
wk = request.form("Week")
'- if you use form method='get', then use request.querystring("Year")
if not isnumeric(yr) then
yr = Year(date) 'or whatever else you want to use as a default
else
yr = CInt(yr)
end if
'similar validation for wk
%>
</head>
<body>
<form method="post" action="thispage.asp">
<select name="Year" size="1">
<%
for i = Year(Date) - 2 to Year(Date) + 2
response.write "<option value='" & i & "'"
if i = yr then response.write " selected"
response.write ">" & i & "</option>"
next
%>
</select> [similar code for week or date or whatever]
<input type="submit">
</form>
<%
If yr <> "" and wk <> "" Then
'- look up stuff in database and output the desired data
'- (this part will be much longer than this)
Else
Response.Write "<p>Please make your selections above.</p>"
End If
%>
</body></html>
If you need to output form fields that are dependent on the user's initial year & week selections, then you're going to need more than one trip to the server, but it's still the same idea: set up the variables you're going to need, see if they have values, write out the form, and then if all the necessary variables have all the necessary values, then you can do your output stuff.

ASP Classic Response buffer exeeded error

This code was working up until today now I keep getting the buffer exceeded error. I'm positive there is a much better way to do this but I have no idea how.
What I'm trying to do is display any entry from the current date to two weeks out. Users can enter any date within that two week period and the table will fill the spaces in between or after with a default "GREEN" span. I had it working until today. I haven't touched it in three weeks and I have no idea what happened. I'm a lowly graphic designer who's bosses don't know the difference between html/css and asp/sql driven applications. Please help before I go insane...
<div class="cond_holder">
<div class="dir_name">PEDS CARDIOLOGY</div>
<%
Dim this_day_peds_cardio
this_day_peds_cardio = Date
Dim Conditions_peds_cardio
Dim Conditions_peds_cardio_cmd
Dim Conditions_peds_cardio_numRows
Set Conditions_peds_cardio_cmd = Server.CreateObject ("ADODB.Command")
Conditions_peds_cardio_cmd.ActiveConnection = MM_webdbs_STRING
Conditions_peds_cardio_cmd.CommandText = "SELECT * FROM dbo.ryg_conditions WHERE aoc='1' AND Day >= DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0) ORDER BY aoc ASC, Day ASC"
Conditions_peds_cardio_cmd.Prepared = true
Set Conditions_peds_cardio = Conditions_peds_cardio_cmd.Execute
Conditions_peds_cardio_numRows = 0
Dim Repeat_peds_cardio__numRows
Dim Repeat_peds_cardio__index
Repeat_peds_cardio__numRows = 14
Repeat_peds_cardio__index = 0
Conditions_peds_cardio_numRows = Conditions_peds_cardio_numRows + Repeat_peds_cardio__numRows
While ((Repeat_peds_cardio__numRows <> 0) AND (NOT Conditions_peds_cardio.EOF))
If DateDiff("d", (Conditions_peds_cardio.Fields.Item("Day").Value), this_day_peds_cardio)=0 Then
%>
<span class="daily_condition <%=(Conditions_peds_cardio.Fields.Item("ryg").Value)%>">
<span style="display: none;"><%=(Conditions_peds_cardio.Fields.Item("aoc").Value)%></span>
<%=(Conditions_peds_cardio.Fields.Item("ryg").Value)%>
<span class="reason"><%=(Conditions_peds_cardio.Fields.Item("reasoning").Value)%></span>
</span><!-- /.daily_condtion -->
<%
this_day_peds_cardio = DateAdd("d", 1, this_day_peds_cardio)
Else
While DateDiff("d", (Conditions_peds_cardio.Fields.Item("Day").Value), this_day_peds_cardio)<>0
%>
<span class="daily_condition GREEN">GREEN</span><!-- SPACER -->
<%
this_day_peds_cardio = DateAdd("d", 1, this_day_peds_cardio)
Wend
%>
<span class="daily_condition <%=(Conditions_peds_cardio.Fields.Item("ryg").Value)%>">
<span style="display: none;"><%=(Conditions_peds_cardio.Fields.Item("aoc").Value)%></span>
<%=(Conditions_peds_cardio.Fields.Item("ryg").Value)%>
<span class="reason"><%=(Conditions_peds_cardio.Fields.Item("reasoning").Value)%></span>
</span><!-- /.daily_condtion -->
<%
this_day_peds_cardio = DateAdd("d", 1, this_day_peds_cardio)
End if
Repeat_peds_cardio__index=Repeat_peds_cardio__index+1
Repeat_peds_cardio__numRows=Repeat_peds_cardio__numRows-1
Conditions_peds_cardio.MoveNext()
Wend
While loop_ctr_peds_cardio < 14
%>
<span class="daily_condition GREEN">GREEN</span><!-- FILLER -->
<%
loop_ctr_peds_cardio = loop_ctr_peds_cardio +1
Wend
%>
</div><!-- /#cond_holder -->
When I read your source code, I came up with two thinks:
You use too many script tags (<% %>), even to seperate vbscript code. This overusing makes it hard to read and understand your code. I had to paste your source code into Notepad++ to tidy up and reading your code.
You didn't use a recordset for the first while query. If you want loop through a result of a Selectquery use the recordset object. It is more convinence to handle and prevents some general errors. Does the using of a recordset eleminate your error?
To your problem:
Do you use an IIS6.0 or higher? If so then following ideas could help (I got it from a german! site of Microsoft, posted in stackoverflow.com (see here). The idears are:
Using Response.Flush()
Turn the Response.Buffer off on the page, or on the entire site.
Response.Buffer = False at the top of the page before any ASP code.
Increase the size of the buffer (see the link on 'see here' position).
Decrase the size of your response.
The reason your response buffer is overflowing is because you have more data to show now. The quickest way to get it sorted should be to issue a Response.Flush every couple of rows or so (depending on how big the response buffer is) inside your while loop. Turning off the response buffer will almost always result in the page taking longer to render, especially if you have lots of context switches like you do.

asp date query formatting

i want to make an asp query so that an event is shown when it's date is greater or equal.
here's the code so far, but it doesn't work.
<%
strDateNow = date
strDateEvent = "30.05.2011"
%>
<% if strDateEvent >= strDateNow then %>
HELLO
<% end if %>
thanks for any help,
alex
I assume this is VBScript + Classic ASP rather than .net?
strDateEvent is a string so the >= is not comparing dates.
To compare against strDateNow which is a date despite its name, you need to convert strDateEvent to a date in order to compare:
If CDate(strDateEvent) >= strDateNow Then
If this fails with a type error then the format "30.05.2011" cannot be converted so use another; "10/04/2011" (ensuring dmy order is appropriate for your locale)

Gridview time format

I have a BoundField that's using a Datafield linked to a datetime type variable. I want to display only the time, not the date. How do you show the time in 24-hour format or in AM/PM format, depending on a boolean in the code behind.
Thanks.
Try something like this:
<%# Eval("AmPmMode").ToString().Equals("true")) ?
String.Format("{0:hh}:{0:mm} {tt}", Eval("date")) :
String.Format("{0:HH}:{0:mm}", Eval("date")) %>
You can format your date for AM PM mode or for 24h mode. tt is the AM/PM designator.
AmPmMode is flag from you DB, should be true or false, but you could change it to 0 or 1.
See also:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Separate date and time form fields in Rails

I have an ActiveRecord model Eventwith a datetime column starts_at. I would like to present a form, where date and time for starts_at are chosen separately (e.g. "23-10-2010" for date and "18:00" for time). These fields should be backed by the single column starts_at, and validations should preferably be against starts_at, too.
I can of course muck around with virtual attributes and hooks, but I would like a more elegant solution. I have experimented with both composed_of (rdoc), and attribute-decorator (lighthouse discussion, github) without success.
Below is a rough outline of what I would like..
class Event < ActiveRecord::Base
validates_presence_of :start_date
end
# View
# On submission this should set start_date.
form_for #event do |f|
f.text_field :starts_at_date # date-part of start_date
f.text_field :starts_at_time_of_day # time-of-day-part of start_date
f.submit
end
Any help appreciated.
Do you have to have a text_field in the view?
As far as I can tell, you can have a date_time field and then just use two different input fields to set the different parts of the field.
form_for #event do |f|
f.date_select :starts_at
f.time_select :starts_at, :ignore_date => true
f.submit
end
Since the rails date and time select helpers set five different parameters (starts_at(1i) for the year part, 2i for the month part, and so on), that means that the date_select only sets them for the date part, while if you pass :ignore_date => true to the time_select, it will only set the hour and minute part.
If you must have a text_field I'm not sure how to do it, but it might be possible to do using some jQuery magic before setting the datetime parameters before sending the form.
Was looking at this today for a Rails project, and came across this gem:
https://github.com/shekibobo/time_splitter
Setting DateTimes can be a difficult or ugly thing, especially through a web form. Finding a good DatePicker or TimePicker is easy, but getting them to work on both can be difficult. TimeSplitter automatically generates accessors for date, time, hour, and min on your datetime or time attributes, making it trivial to use different form inputs to set different parts of a datetime field.
Looks like it would do the job for you
Using a date_select and time_select is a good way to go.
However, I wanted a text_field for the date (so I can use a JavaScript date picker).
Using strong_parameters or Rails 4+:
models/event.rb
# Add a virtual attribute
attr_accessor :start_at_date
views/events/_form.html.haml
- # A text field for the date, and time_select for time
= f.label :start_at
= f.text_field :start_at_date, value: (f.object.start_at.present? ? f.object.start_at.to_date : nil)
= f.time_select :start_at, :ignore_date => true
controllers/events_controller.rb
# If using a date_select, time_select, datetime_select
# Rails expects multiparameter attributes
# Convert the date to the muiltiparameter parts
def event_params
if !!params[:event] && (params[:event]["start_at(4i)"].present? || params[:event]["start_at(5i)"].present?)
if params[:event][:start_at_date].present?
start_at_date = params[:event][:start_at_date]
else
start_at_date = Date.today
end
year = start_at_date.match(/^(\d{4})[\-\/]/)[1]
month = start_at_date.match(/[\-\/](\d{2})[\-\/]/)[1]
day = start_at_date.match(/[\-\/](\d{2})$/)[1]
params[:event]["start_at(1i)"] = year
params[:event]["start_at(2i)"] = month
params[:event]["start_at(3i)"] = day
end
...
Elegant solution may provide date_time_attribute gem:
class MyModel < ActiveRecord::Base
include DateTimeAttribute
date_time_attribute :starts_at
end
It will allow you to set starts_at_date and starts_at_time separately:
form_for #event do |f|
f.text_field :starts_at_date
f.text_field :starts_at_time
f.submit
end
# this will work too:
form_for #event do |f|
f.date_select :starts_at_date
f.time_select :starts_at_time, :ignore_date => true
f.text_field :starts_at_time_zone
f.submit
end
# or
form_for #event do |f|
f.date_select :starts_at_date
f.text_field :starts_at_time
f.submit
end
It will also allow you to play with time zones, use Chronic etc.
Is this because of a design issue? It is really much easier if you just save starts_at as a datetime data type and use something like:
http://puna.net.nz/timepicker.htm#
It simply runs on top of whatever date fields you have for your model.

Resources