Remove 1 day from date - vb.net

I have the date working fine, and I know how to remove a date when doing it one way. But, I need the date in a special format, so I am trying to figure out how to remove 1 day from it with the following code:
TDate.Text = DateTime.Now.ToString("MM/dd")
Ydate.Text = Datetime.Now.ToString("MM/dd")
How can I remove 1 day from the Ydate.text?

AddDay(-1)
so
DateTime.Now.AddDays(-1).ToString("MM/dd")

To remove one day use the AddDays function with a negative number.
Dim _today As Date = Date.Now
Dim _yesterday = _today.AddDays(-1)
Your example:
Ydate.Text = Datetime.Now.AddDays(-1).ToString("MM/dd")

Related

How to change date format vb.net 2015

I want to filter SQL-table between start date and end date, I used before string variable then I use string.format to make the format mm/dd/yyyy, I tried now in VB.net 2015 the following code:
Dim S as String
s=inputbox("Enter Start date")
S=string.format(S,"mm/dd/yyyy")
But it doesn't work, can somebody give me a solution?
You could try this for handling the input value, assuming you only need the date value as a formatted string, since your question is about formatting a date:
Dim S As String
S = InputBox("Enter Start date")
If IsDate(S) = True Then
Dim d As Date = Date.Parse(S)
S = d.ToString("mm/dd/yyyy")
Else
'Handle the non date input here
End If
But I think you should consider #Plutonix comment, since we don't know exactly how you are sending the date to perform the filtering, or how your table fields are defined.
Regards!

How do i add 1 month to a date that already has been created on VB?

Hi I'm new to coding and am looking for some help! I've been trying to add 1 month to a date that I've already stored in my database and displayed on a gridview.
Dim dueDate As DateTime = lblDateA.Text.AddMonth(1)
I know the way that I've done it is wrong but i hope you get the idea i'm going for! Thanks in advance!
First convert the date to a DateTime and add Month to it.
Dim dueDate As DateTime = Convert.ToDateTime(lblDateA.Text).AddMonths(1)
Firstly, the Text of a Label is a String, so you can't add a month to it. Whatever you're displaying in that Label should already be being stored in a DateTime variable somewhere. You would then add one month to that and then display the new value, e.g.
Me.dateA = Me.dateA.AddMonths(1)
lblDateA.Text = Me.dateA.ToShortDateString()
You convert the text to a Date and then add the month.
Private Function ConvertDGVDateAndAddMonth(dateAsString As String) As Date
Dim dt As Date
If Date.TryParse(dateAsString, dt) Then
'parsed correctly so we can use the dt variable as a date
Return dt.AddMonth(1)
End If
'if it does not convert return Nothing
Return Nothing
End Function
USage:
Dim dt As Date = ConvertDGVDate(dgv.CurrentCell.Value.ToString)
'if this does not convert dt = Nothing
'be sure to check

How to compare dateformat rather than using of time

How can I compare the last characters using of month, day, and year or the completedate rather than using datetime, Example case is Textbox3 is greater than textbox9 because Textbox3 day = 26 and textbox9 day = 25.
Mycode:
'in my case I have 2 Textbox.
'Date format: hh.mm MM/DD/YYYY
'Textbox3= 02.02 03/26/2014
'TextBox9= 21.01 03/25/2014
If Val(Strings.Left(TextBox9.Text.Trim, 5)) < Val(Strings.Left(Textbox3 .Text.Trim, 5))Then
TimeError.ShowDialog()
End If
Really, the fastest, most reliable, and most effective way to do this is to parse the values into a DateTime. And taking a step back from there, the fastest, most effective way to get a date time from a textbox is to use a DateTimePicker control.
But if that's not an option, we can build on the code I gave you last time:
Dim temp1() As String = Textbox3.Text.Trim().Split(" .".ToCharArray())
Dim temp2() As String = Textbox9.Text.Trim().Split(" .".ToCharArray())
If DateTime.Parse(temp2(2)) < DateTime.Parse(temp1(2)) Then
TimeError.ShowDialog()
End If
I'll add that you probably want to also have code to compare the time values in the case where the date portions are equal. Given this as a starting point, you should be able to write that code on your own.
in vb.net you can comopare dates like this:
dim date1 as date = cdate(Textbox1.text)
dim date2 as date = Date.now()
if date1.date=date2.date then ....
and months like this
if date1.month=date2.month then ...

Rails 3 - Add months to a date string

I'm trying to add a number of months to a date.
I've tried numerous ways of converting the date string and adding the months but I'm getting nowhere, very fast.
I have start_date which is a date string "YYYY-MM-DD"
And I have item.product.sub_period which is the number of months I'd like to add.
I've tried many ways but am unsure as the correct way to convert the string into a format that can be modified.
Any help is appreciated.
Try the following:
Date.strptime(start_date, "%Y-%m-%d") + item.product.sub_period.months
Just make sure item.product.sub_period returns an integer.
Neal, I believe you can do this:
require 'date'
x = 3
a = Date.new(2012,2,3)
b = a >> x
as long as your date is a Date.

Convert Time to a string format VB.net

I am trying to format a date to a specific string and the code is returning incorect outcome.
CDate(RDate).ToString("ddMMy")
is returning the year as 2 digits is there a way to make it return the correct just last digit ? I know that I could substring to get the result but I would like to use a format string to allow customer to create their own.
Just glue them together with String.Format():
Dim dt = CDate(RDate)
Dim s = String.Format("{0}{1}", dt.ToString("ddMM"), dt.Year Mod 10)
The 'y' just indicates that the leading 0 is to be removed if the year is between 0 and 9. There is no format for just getting the last digit of the year.
See the MSDN documentation for the available custom formats.
The Custom Format year formats are limited to returning a minimum of a 2 digit year except in the situation that Compentent_tech mentions where you are using y and the year is ending with a 00 to 09. you can check out the MSDN Documentation. You will need to use string manipulation to achieve what you want.
Using that fact you can try something like this.
Dim RDate As Date = Now.Date
Dim NDate As Date = New Date((RDate.Year Mod 10), RDate.Month, RDate.Day)
CDate(NDate).ToString("ddMMy")
try this code :
Dim dt As DateTime = DateTime.Now
CDate(dt).ToString("ddMMyy").Remove(4, 1)
this code removes first of 2 digit year value.