Adding Hours to DateTimePicker in VS2010 - visual-studio-2010

How to add 3 hours in DateTimePicker in VS?
Dim dateAdd as Date = DateTimePicker1
dateAdd.AddHours(3)
MsgBox(dateAdd.Value.ToString)
Why does it doesn't add on my system?

DateTime.AddHours() returns a new DateTime:
Dim dateAdd as Date = DateTimePicker1
Dim newDate as Date = dateAdd.AddHours(3)
MsgBox(newDate.Value.ToString)

Related

How to get next year date from the user entered date in java

Below is the code that returning wrong date
String dt = "01-08-2021";
SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
Date date = new SimpleDateFormat("dd-mm-yyyy").parse(dt);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
cal.add(Calendar.YEAR, 1);
date = cal.getTime();
System.out.println(format.format(date));
It printing as 31-08-2021, it suppose to print 31-07-2022.
If I pass 02-08-2021, it working perfectly 01-08-2022
I am using java1.7. Can anyone help me on this.
Not sure whether thats the issue... but: you're using the SimpleDateFormat wrong. MM stands for month. mm stands for Minute. See here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You do not need line - cal.add(Calendar.DATE, -1) in your code.
You can try below code, it working fine
String dt = "01-08-2021";
SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
Date date = new SimpleDateFormat("dd-mm-yyyy").parse(dt);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, 1);
date = cal.getTime();
System.out.println(format.format(date));

Display message in date range VbScript

I am trying to display a message based on a date range. If it falls between the BeginDate and EndDate then the date should be display and if the date falls on 10-09-2017 then another message should be displayed, otherwise then it should display the date. This seems to fail and go directly to the else statement. My eyes are not picking up the error. How can something get displayed between a date range in VBScript.
<%
Dim DateT
Dim BeginDate
Dim EndDate
BeginDate = Day("2017-05-26")
EndDate = Day("2017-11-04")
DateT = Day(Date)
If BeginDate >= DateT =< EndDate
THEN response.write(DateT)
ElseIF BeginDate = Day("2017-10-09")THEN
response.write(DateT)
Else
response.write(DateT)
End If
%>
I believe your syntax is incorrect and your logic is overcomplicated.
Not exactly sure of the syntax here or the correct date format but I added some response.write for you to check (which should have been your first stop)
Your logic is overcomplicated because the only time it doesn't display the date is when it matches that special date..
Dim DateT
Dim BeginDate
Dim EndDate
BeginDate = CDate("2017-05-26")
EndDate = CDate("2017-11-04")
DateT = Date()
' remove these when you're finished debugging
response.write(BeginDate)
response.write(EndDate)
If BeginDate = CDate("2017-10-09")THEN
response.write("Another message")
Else
response.write(DateT)
End If

How to get yesterday's date vbscript

Date format Shows Invalid Date With Custom Date. When I use 'Date' instead of "25th May" it shows yesterday's Date.
function GetYD()
Dim dt, yesterday
dt = DateAdd("d", -1, "25th May")
yesterday = Right(Year(dt),2) & Right("0" & Month(dt),2) & Right("0" & Day(dt),2)
msgbox yesterday
GetYD = yesterday
end function
Be sure to feed it a string that can be parsed. Always use digits if possible, and you did not even specify a year.
The format yyyy-mm-dd would probably work best, as it is (both to humans and to computers) totally un-ambiguous. So try it using
DateAdd("d", -1, "2017-05-25")

Date Validation: Setting minimum and maximum date in a Textbox

I have a function in my VBA code which sets a specific Date format for a textbox.
This is my code to verify the Date is in the correct format:
Function CheckDate(DateStg As String) As Boolean
If DateStg = "" Then
' Accept an empty value in case user has accidentally moved to a new row
CheckDate = True
lblMessage.Caption = ""
Exit Function
End If
If IsDate(DateStg) Then
CheckDate = True
lblMessage.Caption = ""
Else
CheckDate = False
lblMessage.Caption = "Sorry I am unable to recognise " & DateStg & " as a date."
End If
End Function
In addition to checking if the date in the textbox is an actual date, I need to verify that the textbox date is not less than the current date minus 1 month, and. Also, I would like to verify that the date is not more than the current date plus 1 year.
So:
DateStg > Today - 1 month
DateStg < Today + 1 year
Thanks for your help in advance.
You have a few functions you can use:
''Assume date is not good
DateOK=False
If IsDate(DateStg) Then
If DateStg > dateAdd("m",-1,Date()) _
And DateStg < dateAdd("m",12,Date()) Then
''Date is good
DateOK=True
End If
End if
For the most part, textboxes can be set to only accept dates and you can set validation rules to check the range, so code may not be necessary.
If you just want to check the date, you can use the DateAdd-function to get the dates to compare:
'Subtract a month from today and return it as a string
Format(DateAdd("m", -1, Now), "yyyy-mm-dd")
'Add a year to today and return it as a string
Format(DateAdd("yyyy", 1, Now), "yyyy-mm-dd")

RowFilter using Dates VB.NET

I have this dates into my DGV:
14/06/2012
15/07/2012
16/07/2012
17/07/2012
I set the Filter to this: ([supplier_invoice_date] >= '13/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')
The Filter return this (All the dates):
14/06/2012
15/07/2012
16/07/2012
17/07/2012
Another test:
Filter: ([supplier_invoice_date] >= '15/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')
Result:
15/07/2012
16/07/2012
17/07/2012
Filter: ([supplier_invoice_date] < '17/06/2012')
Result:
14/06/2012
15/07/2012
16/07/2012
I think it is only taking the days and does not take months.
Here is my code:
Dim dt As New DataTable
Dim suppliersinvoices_data_query As String = ("DATE_FORMAT(MIN(supplier_invoice_date), '%d/%m/%Y') AS supplier_invoice_date, ...")
Dim invoice_objDataAdapter As New MySqlDataAdapter(suppliersinvoices_data_query, objConn)
invoice_objDataAdapter.Fill(dt)
Dim MyFilter As New DataView(dt)
MyFilter.RowFilter = "([supplier_invoice_date] >= '13/07/2012') AND ([supplier_invoice_date] <= '17/07/2012')"
invoicesresults_datagrid_search_supplierinvoice.DataSource = MyFilter
This is the DataView.RowFilter syntax for Dates:
dataView.RowFilter = "supplier_invoice_date >= #2012-07-13#"
But you could also using Linq-To-DataSet. I assume that the actual datatype of the field isStringinstead of Date, so you need to parse it to Date first:
Dim startDate = New Date(2012, 7, 13)
Dim endDate = New Date(2012, 7, 17)
Dim invoiceDate As Date
Dim filtered = From row In dt.AsEnumerable()
Where Date.TryParse(row.Field(Of String)("supplier_invoice_date"), invoiceDate) _
AndAlso invoiceDate >= startDate AndAlso invoiceDate <= endDate
Dim tblFiltered As DataTable = filtered.CopyToDataTable()
It looks like you are converting the dates to strings in order to format them. Try to avoid that, and just let the grid use the date value it is getting from the data source.
If you want to format the date of the DataGridView control, try it like this:
dgv1.Columns("YourDateColumn").DefaultCellStyle.Format = "dd/MM/yyyy"
Then you can use real dates to filter the data:
MyFilter.Filter = String.Format("[supplier_invoice_date] > '{0}'", _
New DateTime(2012, 7, 1))

Resources