i am developing web application in vb.net, using Custom validation how to validate the from date and To date..
i need to validate the Date in the asp:textbox should be "dd/mm/yyyy" format and i have two date like from date and Todate. So the Todate should not be less then start date, Can anyone please help me,
Thanks in advance.
This is VB.Net Way to do it. You can modify it to fit asp.net
Dim date1 As DateTime = "01/01/2000"
Dim date2 As DateTime = "07/09/2012"
Dim myDate As DateTime = InputBox("Date?")
If myDate >= date1 AndAlso myDate <= date2 Then
MsgBox("Between")
Else
MsgBox("Not between")
End If
Related
Good afternoon, I'm new to programming and I'm working with VB.NET.
I need to get the difference between two dates and then list all the intermediate dates in listbox1. I tried the following code but it doesn't work.
Private Sub breaks()
Dim date1 As Date = DateTimePicker1.Value.ToString("dd/MM/yyyy")
Dim date2 As Date = DateTimePicker2.Value.ToString("dd/MM/yyyy")
While date1 <= date2
Dim result = date1
ListBox1.Items.Add(result)
Dim term = 1
date1 = DateTimePicker1.Value.AddDays(term)
End While
End Sub
The function is called within a button. When executed it only shows the sidebars but is blank.
The image shows start date 03/10/2020 and end date 03/16/2020, however the result (listbox) does not return anything.
I expected my result to come:
03/10/2020
03/11/2020
03/12/2020
03/14/2020
03/15/2020
03/16/2020
the interval between them.
Can anyone tell me what's wrong?
You can use some linq for a simple solution
ListBox1.DataSource =
Enumerable.Range(0, 2 + DateTimePicker2.Value.Subtract(DateTimePicker1.Value).Days).
Select(Function(offset) DateTimePicker1.Value.AddDays(offset)).
ToList()
It generates a list of numbers to act as the offset from the initial date, then adds them the specified number of times (different between the dates in days) to create all the dates. No loop required.
Credit to this answer
Edit:
This can also be similarly applied to a DataGridView, but in order to make a single column, we would need to select an anonymous type.
DataGridView1.DataSource =
Enumerable.Range(0, 2 + DateTimePicker2.Value.Subtract(DateTimePicker1.Value).Days).
Select(Function(offset) New With {.Date = DateTimePicker1.Value.AddDays(offset)}).
ToList()
You should avoid using strings for datetimes until they need to be text.
The variable date1 can be used for all the dates, like this:
Dim date1 As Date = DateTimePicker1.Value
Dim date2 As Date = DateTimePicker2.Value
While date1 <= date2
ListBox1.Items.Add(date1.ToString("MM/dd/yyyy"))
Dim term = 1
date1 = date1.AddDays(term)
End While
Also, you should make sure to set Option Strict On as the default for new projects, and set it for the current project.
I need help to filter between two dates and it's giving me an error:
Named argument not found
my code
Dim StartDate As String
Dim EndDate As String
'
StartDate = Date
EndDate = Date + 365
'
ActiveSheet.AutoFilter Field:=7, Criteria1:=">=StartDate", Operator:=xlAnd, Criteria2:="<=EndDate"
Any help would be much appreciated!
if your dates are actual Date values (i.e. not String ones looking like dates), then go like this:
Dim StartDate As Date
Dim EndDate As Date
StartDate = Date
EndDate = Date + 365
ActiveSheet.Range("A1:AO1").AutoFilter Field:=7, Criteria1:=">=" & CDbl(StartDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(EndDate)
I posted a sample file on my google drive.
https://drive.google.com/open?id=0B2w6b7-P-pX1R0dlTlFUSl9xZlE
I can describe the logic, but it's just easier for you to download it, play around with it, and see the mechanics of it.
Basic logic was the issue... mix up of the start and end date logic (should be EndDate = Date and StartDate = Date - 365).
Sorry for the troubles!
In a VBA project i'm working on, I need to count the number of lines where the difference between two dates is inferior to X hours.
My idea was to go through all the lines and check for each line, like this:
Dim count as Integer
if DateDiff("h", date1, date2) < 24 then
count = count + 1
End If
The problem is that I get an incompatibility type error (execution error 13).
Is it possible to make IF statements on a DateDiff function? Or is it maybe possible to make a filter with DateDiff as the condition?
Thanks for the help and sorry for my poor english as it's not my main language!
I have tested it like this without any errors.
Dim date1 As Date
Dim date2 As Date
date1 = "14/10/2015 19:00:00"
date2 = "14/10/2015 16:28:43"
If DateDiff("h", date1, date2) < 24 Then
count = count + 1
End If
The VBA conversion function CDate can apply a little of the overhead you constantly pay for to correct the dates. With that said, it is best to correct the data to begin with and not rely upon error controlled conversion functions.
Dim date1 As Date, date2 As Date, n As Long
date1 = CDate("14/10/2015 19:00:00")
date2 = CDate("14/10/2015 16:28:43")
Debug.Print date1 & " to " & date2
If IsDate(date1) And IsDate(date2) Then
If DateDiff("h", date1, date2) < 24 Then
n = n + 1
End If
'alternate
If date2 - date1 < 1 Then
n = n + 1
End If
End If
Given that 24 hours is a day and a day is 1, simple subtraction should be sufficient for this base comparison.
Your DMY format may cause problems with the EN-US-centric VBA in date conversion. If an ambiguous date string is found (e.g. "11/10/2015 16:28:43") you may find that you are interpreting it as MDY.
In the following DataView.Rowfilter filter, Request_Date is a smalldatetime:
dv.RowFilter = "Request_Date >= '01/01/2012' and Request_Date <= '12/31/2012'"
The problem with this is that smalldatetime is MM/dd/yyyy hh:mm:ss, but it is compared to a string with the format 'MM/dd/yyyy'. This means that the filter will automatically convert the strings to smalldatetime, so the comparison only shows date/times between 1/1/2012 at 12AM and 12/31/2012 at 12AM. Any rows with dates later in the day on 12/31/2012 will not get picked up by this filter. I know that I can add a day to the end date or concatenate, say, 12:59:59 to the end of the date to pick up the other times in the day, but I was hoping for somthing more elegant, along the lines of the sql equivalent ...CONVERT(smalldatetime, Request_Date, 101) <= '12/31/2012'. Is there any way that I can get a different date format for a DataView field or am I stuck massaging the end date prior to comparison?
FYI, current best option is this:
dv.RowFilter = "Request_Date >= #" & dtpStartDate.DateText & "# and Request_Date <= #" & DateAdd(DateInterval.Day, 1, dtpEndDate.DateValue) & "#"
Thanks for your help!
If you're using at least .NET 3.5, you can use Linq-To-DataSet which is more readable:
DataTable filtered = dv.Table
.AsEnumerable()
.Where(r => r.Field<DateTime>("Request_Date") >= dtpStartDate.Value
&& r.Field<DateTime>("Request_Date") < dtpEndDate.Value.AddDays(1))
.CopyToDataTable();
Add using.System.Linq; and a reference to System.Data.DataSetExtensions.dll.
Edit: I've only just seen that VB.NET is tagged:
Dim filtered = From row In dv.Table
Where row.Field(Of DateTime)("Request_Date") >= dtpStartDate.Value AndAlso _
row.Field(Of DateTime)("Request_Date") < dtpEndDate.Value.AddDays(1)
Dim tblFiltered = filtered.CopyToDataTable()
Instead of using "<= 12/31/2012", just use "< 1/1/2013" - that is the most elegant and gets exactly what you want.
I have following code and I can filter data on grid but when I pick same date in 2 datepicker it shows nothing. How can I fix the problem. Any help will be appreciated.
con = New SqlCeConnection(constring)
con.Open()
cmd = New SqlCeCommand("SELECT * FROM tblMeter WHERE (Date >= #startDate) AND (Date < #endDate)", con)
Dim param1, param2 As SqlCeParameter
param1 = New SqlCeParameter("#startDate", DateTimePicker1.Value)
param2 = New SqlCeParameter("#endDate", DateTimePicker2.Value)
cmd.Parameters.Add(param1)
cmd.Parameters.Add(param2)
Dim da As New SqlCeDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
con.Close()
DataGridView1.DataSource = dt
Thanks
param2 = New SqlCeParameter("#endDate", DateTimePicker2.Value.AddDays(1))
Remember that Sql Server interprets a date like this: 2010-06-23
as a date like this: 2010-06-23 00:00:00.000.
In other words, even if you use >= and <= to check the range at both ends you're still only giving yourself a one millisecond timespan. Sometimes you get away with that if all the dates in the column have no time component, but it's rarely what you really want. Instead, you need to add one day to the end of your range so that your filter includes the entire day, and not just the first millisecond.
That's because #endDate implies the time 00:00, to include the whole day add the time 23:59:59. Or add 1 to #endDate.
Change your query to less-than or equal-to your end date.
SELECT * FROM tblMeter WHERE (Date >= #startDate) AND (Date <= #endDate)
It's your logic:
If date is 2001 and you input 2001:
2001 >= 2001 - check
2001 < 2001 - Nope
Try changing the exclusive
(Date < #endDate)
to the inclusive
(Date <= #endDate)
You may use:
param1 = New SqlCeParameter("#startDate", DateTimePicker1.Value.ToShortDateString)
param2 = New SqlCeParameter("#endDate", DateTimePicker2.Value.AddMinutes(1))