VBA - Get the day before when a date is specified - vba

I'm currently trying to write a piece of code where the user is giving a text box to enter a date in the format dd/mm/yyyy and once that has been specified, I want to get the day before that date.
For example, the user enters 14/12/2018 and I would like VBA to store 13/12/2018 to another variable.
Probably a stupid query, but would be handy to know if this is possible.

Nevermind, I figured it out!
Here's what I did in case anyone wants to know:
Dim FirstRun as Date
Dim FirstRunBefore as Date
FirstRun = InputBox("Enter date")
FirstRunBefore = Format(DateAdd("d", -1, FirstRun), "dd mmmm yyyy")

Related

Getting the OLE value from a String with a date. (VB.Net)

My first question on StackOverflow. Hope it gets recieved okay. :)
So I have a form with 3 text boxes.
Date:
Format:
Output:
Lets say you write the date like this "05/07/1994"
The problem i have when converting a date like that, is the culture support.
Is 05 the month and is 07 the day? This varies from country to country.
I wanna be able to write it in the "format" textbox. Like this:
Date: 05/07/1994
Format:dd/mm/yyyy
Dim strDate As String = String.Empty
Dim strFormat As String = String.Empty
Dim strResult As String = String.Empty
'I got the code to split the "strDate" by every "/" or every "." so i can isolate the days/months/years in a list.
How would you create the logic, so that it reads the "format" and then handles the date in the way that the user want it
Thanks in advance! Please tell me if i need to formulate it in a different way.

System.FormatException: 'String was not recognized as a valid DateTime.'

I have a problem on VB.Net which a part of my system has a error. It doesn't save the date thus generating this error. The format was identical to the output on the datetime Picker.
ElseIf Date.Parse(expirationDTP.Value.ToString("MM/dd/yyyy")) <= Date.Parse(Date.Now.ToString("MM/dd/yyyy")) Then
MsgBox("Select the expiration date of the stock!")
This is an image of the output.
Date Output
Thanks!
expirationDTP.Value and Date.Now already are of type Date. Why are you converting them into strings only to convert them back to Date objects?
Just compare them directly:
ElseIf expirationDTP.Value <= Date.Now Then
For future reference, the actual error occurs because Date.Parse() tries to use the current system's cultural settings to parse the date. If you need to parse a specific date format you should use Date.ParseExact():
Date.ParseExact("01/20/2018", "MM/dd/yyyy", CultureInfo.InvariantCulture)
EDIT:
Try comparing the Date properties of the two Date objects:
ElseIf expirationDTP.Value.Date <= Date.Now.Date Then
If it still doesn't work you're going to have to check if it's any preceding ElseIf or the initial If that gets executed instead. As you haven't shown any more code than that there's not much more I can do.

Working with Excel Dates in vb.net

I am experiencing a problem when attempting to read a date from an excel sheet. (The date column is formatted the same as the short date format of the computer its opened on). I populate the dates from the excel sheet into a datagrid with success, but when I attempt to parse the date (To format it appropriately), I get a error saying the string wasn't a valid DateTime value. The computer's short date format is dd/MM/yyyy. This is the code I use to try parsing the date. The following code is an example of where the process fails.
Dim dateParsed AS DateTime = DateTime.Parse("14/01/2013").ToString("yyyy-MM-dd")
Is there some way to programatically get the system's short date format and use ParseExact instead or any suggestions?
You want to parse exact, using the current cultures short-date format?
Dim dt As DateTime = DateTime.ParseExact(str, "d", CultureInfo.CurrentCulture)
Additionally, if you actually need to see the ShortDate format for yourself get it through the CurrentCulture.
CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
You can try this piece of coding
Dim dt As DateTime = DateTime.Parse("1/14/2013").ToString
Dim f As String = Format(dt, "yyyy-MM-dd")

Date not converting correctly in VB.net

It seems like I keep having problems with dates. I am using the following code:
Dim LocalDateCultureProvider As New CultureInfo(CultureInfo.CurrentCulture.ToString)
Dim CurrentDate As DateTime = Convert.ToDateTime(System.DateTime.Now.ToString("dd/MM/yyyy"), System.Globalization.CultureInfo.InvariantCulture)
ExpiryDate = DateTime.ParseExact(strDate, "dd/MM/yyyy", LocalDateCultureProvider)
If DateTime.Compare(ExpiryDate, CurrentDate) < 0 Then
MsgBox("This file has expired.")
Exit Sub
End If
Here I am reading strDate as a string and for one example, the value of this is "29/09/2012" However, in the ExpiryDate line it converts to #09/29/2012# so that in the comparison with today's date which is stored (correctly in my opinion) in CurrentDate as #10/6/2012# I get the If condition to be true (wrongly).
BTW, I also tried
Dim LocalDateCultureProvider As New CultureInfo(System.Globalization.CultureInfo.InvariantCulture.ToString)
just to see if that was causing the problem. I am trying to build something that will work in all Cultures. No matter what the local settings are, I want to test for expiration by comparing the current system date with an expiration date which I receive as a string. Please tell me how to go about this so I can get consistent results.
TIA,
Chiwda
No, you parse the CurrentDate incorrectly. CultureInfo.InvariantCulture expects the month before the day but you formatted it with the day first. You are writing unnecessary code, simply fix with:
If DateTime.Compare(ExpiryDate, DateTime.Now) < 0 Then

Yet another date formatting problem :(

I seem to have a date formatting problem every day!
I am querying a table and am getting a date back in the format dd/mm/yyyy (as a string btw). Brilliant! thats what I want. But, now I want to convert that string to a date so i can do
dim dayNumber as integer = day.DayOfWeek
But when I convert it to a date it changes it to #m/dd/yyyy#. AHHHH! how can I change this?
here is my code i've tried
Dim ActivityDate As String
If dt.Rows(i)("Date") Is DBNull.Value Then
ActivityDate = ""
Else
ActivityDate = dt.Rows(i)("Date")
End If
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse(ActivityDate, ci)
Dim day As Integer = theDate.DayOfWeek
Cheers
Brilliant! thats what I want
That's not what you want. It is the worst possible format for a date because it is so horribly ambiguous. Date string formats depend on the current culture. "4/1/2010" is Unicorn day at SO, it is day in January in Europe. "#4/1/2010#" is a legacy VB6 format.
Always store dates in a DateTime in your code. Always store dates in a database column type of datetime in your dbase. There is never any ambiguity and you'll have an easy time with the DateTime members to manipulate dates.
If you convert the string to a date, you can always output it back to the original format using a custom format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
The correct solution here (at least until you tell us why this isn't possible) is to update your database to use a datetime column type rather than a varchar. Now we also know that this column has no NULL values, because otherwise you'd be complaining about exceptions on your Date.Parse() call. After applying both those sentences, you can trim all that code down to a simple one-liner:
Dim day As Integer = DirectCast(dt.Rows(i)("Date"), DateTime).DayOfWeek
May I also ask why you're looping through the table row by row? I've worked in a shop where that was the norm, but since I've left there I've run in to alternatives and more and more I'm coming to find looping through a datatable as just wrong. It's an older imperative coding style, and generally you want to go for a declarative coding style.
Are you parsing it like this:
Dim newDate as DateTime = DateTime.Parse(myDate)
If the culture of your system does not use that date format, then you should get that date string as an actual date:
' canadian date format is dd/mm/yyyy
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse("13/04/2010", ci)
Make sure you specify an exact parse format like so:
Console.WriteLine(DateTime.ParseExact("17/12/2010", "dd/mm/yyyy", null));
I am not sure what the last parameter is but it is safe to ignore it.
I'm guessing that you are seeing the #m/dd/yyyy# in the debugger, like this screenshot below. Don't worry!
A Date variable isn't stored as a string. The debugger has to convert your Date into a string to display it, and it insists on showing dates in #m/dd/yyyy# format. But that doesn't have any effect on the runtime behaviour of your program.
Screenshot of Visual Studio Debugger http://img707.imageshack.us/img707/6205/debugger.gif