Date Formatting in Classic ASP - asp-classic

I'm writing in Classic ASP for a registration form, and would like to close the registration. I declared the closing date in the variable "closingdate", and have another variable which gets the current date.
dim closingdate : closingdate = "5/17/2014"
response.write("Closing date: " & closingdate & "<br>")
dim currentdate : currentdate = Date
response.write(currentdate)
response.write("<br>")
if (currentdate > closingdate) then
response.write("Registrations are closed")
else
response.write("Registrations are still open")
end if
The results given to me right now is the "Registrations are still open", despite me setting the closing date as a date that's a year before. Have I left out something here?

try casting your string into a date first:
if (currentdate > cdate(closingdate)) then
response.write("Registrations are closed")
else
response.write("Registrations are still open")
end if

Related

Show remaining time for user unlock based on current datetime and LastLockoutDate

I am trying to show a counter for when a user account is automatically unlocked.
I would like the counter stored within unlockCounter to display only the number of minutes remaining.
unlockDate is a DateTime stored in a format such as: 11/29/2014 1:06:05 PM.
EDIT: This is ultimately what I came up with that works perfectly. Updated in case others are interested.
If (CurrentUser IsNot Nothing) Then
If (CurrentUser.IsLockedOut) Then
Dim lastLockout As DateTime = CurrentUser.LastLockoutDate
Dim unlockDate As DateTime = lastLockout.AddMinutes(Membership.PasswordAttemptWindow).AddSeconds(-1)
Dim unlockCounterMinutes As String = unlockDate.Subtract(DateTime.Now).Minutes + 1
Dim unlockCounterSeconds As String = unlockDate.Subtract(DateTime.Now).Seconds
If (unlockCounterMinutes > 1) Then
LoginUser.FailureText = "Your account has been locked - Please try again in " & unlockCounterMinutes & " Minutes"
ElseIf (unlockCounterSeconds > 1) Then
LoginUser.FailureText = "Your account has been locked - Please try again in " & unlockCounterSeconds & " Seconds"
Else
LoginUser.FailureText = "Your account is now being unlocked - Please login again"
End If
End If
End If
Use unlockDate.Subtract(DateTime.Now) to get a Timespan object of the time window left.
Don't let the user in while the TotalMinutes or TotalSeconds is positive depending on the granularity you want.

cType : string to date throwing InvalidCastException (ASP) (VB.NET)

I am getting exception on following lines
InspectionDate = "07/15/2014"
If CType(InspectionDate, Date) > Date.Today Then
'Here comes my logic etc
'Here comes my logic etc
'Here comes my logic etc
End If
When i debugged i see Date.Today = #7/15/2014#
Can any one please help on this.
Thanks..
Try like this
Dim dt As Date = Date.ParseExact("07/15/2014","MM/dd/yyyy", CultureInfo.InvariantCulture);
IF dt > Date.Today Then
'CODE HERE
END IF
Check the full documentation at msdn
InspectionDate = "07/15/2014"
If DateTime.Parse(InspectionDate) > Date.Today Then
'Here comes my logic etc
'Here comes my logic etc
'Here comes my logic etc
End If
try like this
Before compare both dates use the TryParse method
Dim dateValue as Date
InspectionDate = "07/15/2014"
If (Date.TryParse(InspectionDate, dateValue) > Date.Today Then
'your code here
End If
check the metod in the MSDN
check the description

trying to get current date in code behind

I push a button on a web page.
I am trying to get the current date into 3 variables Year, Month, and Day
In the Code behind I Have:
Dim intDay As Integer
intDay = Date.Now.Day
I get error message
Input string was not in a correct format.
Suggestions?
There is nothing wrong with the code you posted.
See this ideone for the same code which compiles and executes just fine.
Dim intDay As Integer
intDay = Date.Now.Day
Console.WriteLine(intDay)
The Now property returns the current time as a DateTime value.
On which you can call the Day property which returns an integer
The error message you got refers to a string which is not in the posted code so I'm guessing the problem is somewhere else
As Iam Asp.Net Developer I have Code Like This....
I Take Three Textboxes For Showing YYYY,MM,DD
TextBox1.Text = DateTime.Now.ToString("yyyy");
TextBox2.Text = DateTime.Now.ToString("MM");
TextBox3.Text = DateTime.Now.ToString("dd");
This Gives Output Like This...
2013
08
08
Shouldn't it be:
intDay = DateTime.Now.Day
Edit: didn't see the comment stating the in VB.NET Date is valid too ;)
This is taken straight from the docs. So you should be able to assign DateTime.Now to moment and get what you want from it.
http://msdn.microsoft.com/en-us/library/system.datetime.day.aspx
Dim moment As New System.DateTime(1999, 1, 13, 3, 57, 32, 11)
' Year gets 1999.
Dim year As Integer = moment.Year
' Month gets 1 (January).
Dim month As Integer = moment.Month
' Day gets 13.
Dim day As Integer = moment.Day
' Hour gets 3.
Dim hour As Integer = moment.Hour
' Minute gets 57.
Dim minute As Integer = moment.Minute
' Second gets 32.
Dim second As Integer = moment.Second
' Millisecond gets 11.
Dim millisecond As Integer = moment.Millisecond
I think you want DateTime.Now.Day

Check for specific date format in vba excel 2010 64-bit

I'm doing some validations to a cell where the user inputs a date value. The correct format I'm expecting is "m/d/yyyy", so I need a way to check that the user enter the date in that format.
How can I achieve that?
here are some of the validations I've made:
Dim StartDate As String
Dim EndDate As String
With Sheet1
StartDate = WorksheetFunction.Trim(.Range("F2").Value)
EndDate = WorksheetFunction.Trim(.Range("F3").Value)
End With
'Dates validations
If StartDate = "" Or EndDate = "" Then
MsgBox ("Dates can't be empty")
Exit Sub
End If
If Not IsDate(StartDate) Or Not IsDate(EndDate) Then
MsgBox ("Please check dates format")
Exit Sub
End If
If CDate(StartDate) > CDate(EndDate) Then
MsgBox ("Start Date can't be greater than End Date")
Exit Sub
End If
Siddharth Rout's solution:
Hope this helps? support.microsoft.com/kb/211485.

Compare two dates in vb.net whether they are equal or not

I have a Date variable startdt and a String variable hdnsdate.
Suppose if startdt has the value in the form 3/1/2012 and hdnsdate has the value in the form 03/01/2012. Than how can i compare that these two dates are equal in vb.net.
In if condition of my program i want to do this check. In case the two dates match move out of if loop otherwise go into the if loop.
E.g A sample in C# what exactly i want.
if(startdt !=hdnsdate)
{
//Do
}
else
{
//Do this
}
Parse hdnsdate (string) to Date type using Parse, ParseExact method and use DateTime.Compare, Equals, CompareTo methods.
String to date
Dim enddate as Date
Date.TryParse(hdnsdate, enddate)
If startdt = enddate Then
'Do this
Else
'Do this
End If
Alternative to compare date:
Dim result = DateTime.Compare(date1, date2)
If result=0 Then
'Do this
End If
You need to parse the string into a DateTime then compare the two.
VB.NET
Dim parsed As DateTime = DateTime.Parse(hdnsdate)
If startdt != parsed Then
Else
End If
C#:
DateTime parsed = DateTime.Parse(hdnsdate);
if(startdt != parsed )
{
//Do
}
else
{
//Do this
}
I suggest looking at the different parsing methods defined on DateTime, as you may need to use a standard or custom date and time format string to ensure the string gets parsed correctly.
Dim result = DateTime.Compare(hdnsdate, date2)
If result=0 Then
'Do this
End If

Resources