Getting the WeekdayName + 1 - vb.net

I am sure this is very easy but I am having one of those days. I have the code Now.Date.AddDays(+1) which gets me the date for tomorrow but I need the week day name for tomorrow. Been googling for a while now and just cant seem to find it.

Use DateTimeFormatInfo.GetDayName
Dim tomorrow = Now.Date.AddDays(1)
Dim weekDayName = CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(tomorrow.DayOfWeek)
You can also use standard datetime format strings:
weekDayName = tomorrow.ToString("dddd") ' long '
weekDayName = tomorrow.ToString("ddd") ' abbreviated'
Standard Date and Time Format Strings: The Long Date ("D") Format Specifier

Simply:
Dim sDayName As String = Date.Today.AddDays(1).ToString("dddd")
You could check the available patterns here.

Related

VBA convert string with day and month name to date

I have a string of a date in french in long format like so:
mardi 7 juillet 2020
How can I convert it to a date type in VBA ? I tried using CDate, DateValue and DateSerial, but I couldn't figure it out. There must be a way if VBA has a Long Date format for dates. I just can't find anyone that was asking this conversion question with this format.
Note that I have a lot of dates since I am looping over many mails so I need a solution that is very general so a solution that takes into account all possible week days (lundi, mardi, ..., dimanche), all possible days 1 to 31 and months Janvier to Décembre and all possible years.
Thank you for your help.
I agree there should be an easier conversion. This function will strip out the weekday (which is superfluous) and then rely on VBA type conversion to get a Date type (NB. I have only tested this under a English(UK) regional setting).
Option Explicit
Public Function DateFromLongFormat(strLongFormatDate As String) As Date
Dim strDate As String
strDate = Right(strLongFormatDate, Len(strLongFormatDate) - InStr(1, strLongFormatDate, " "))
DateFromLongFormat = strDate
End Function
Tried it out using a simple test routine on today's date:
Sub TestDate()
Dim strToday As String
strToday = Format(Now(), "dddd dd mmmm yyyy")
Dim dt As Date
dt = DateFromLongFormat(strToday)
End Sub

String value wrongly converted to datetime using vb.net

So I have this datetime value of 9.3.2016 18:56:12, by using datetime.parse, I can get the values but instead of getting '3' as the month, it takes '9' as month and '3' as day which is incorrect.
dim d1 as string = "9.3.2016 18:56:12"
dim d2 as datetime = datetime.parse(d1, CultureInfo.InvariantCulture)
I don't want to use datetime.parseExact because I'm having more than 1 value in the database.
Please help :( thank you!
Since you current culture appears to interpret dates in the way you expect you could simply do this,
Dim dateString = "9.3.2016 18:56:12"
Dim dateValue = DateTime.Parse(dateString)
By not specifying the InvariantCulture, you instruct DateTime.Parse to use the current culture which, in your case, interprets the date string correctly.
Sorry I've got the solution.
dim d2 as datetime = convert.todatetime(d1)

How do I format a date in a string variable to year date month in vb.net

I have a date in string variable strOrderEndDate which looks like this 8/14/2015.
I would like to convert it to 2015-08-14.
How do I do this in vb.net?
I tried strOrderEndDate.ToString(yyyy mmmm dd) but its not working
As the comments indicate, you first need to convert the string to a date using CDate. Then you can use the .ToString method on the new date type variable to format the output as desired:
Dim strOrderEndDate As String = "8/14/2015"
Dim datOrderEndDate As Date = CDate(strOrderEndDate)
MsgBox(datOrderEndDate.ToString("yyyy-MM-dd"))
...or as Plutonix recommends, you can use DateTime.Parse to perform the conversion. He's pretty smart so if he says it's better, then it's better.
Dim datOrderEndDate As Date = DateTime.Parse(strOrderEndDate)

Formatting the current date

Either I just can't seem to find the right way to word the problem or it's a lot more complicated than I thought, but what is the easiest way to assign the current date to a variable and format it to a specific format? Basically, I want to do the VB.net equivalent of this VBA line:
formatted_date = Format(Date, "yyyy-m-d")
Thanks!
Dim formattedDate as String = DateTime.Now.ToString("yyyy-M-d")
Note that capital M is for month, lowercase m would get you minutes.
If by "Date" you really mean current date (and not an example variable), the VB.NET Equivalent is "Today"
Do one of these things... (whichever you like):
formatted_date = Today.ToString("yyyy-M-d")
formatted_date = String.Format("{0:yyyy-M-d}", Today)
If your "Date" was just an example variable, just replace "Today" in the above examples with your variable name.
to simply : SomeDate.ToString("yyyy-m-d")
Try this
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))

How to get a date field from MM/dd/yyyy to yyyy/MM/dd in vb.net

I need to get a date field from MM/dd/yyyy to yyyy/MM/dd in vb.net but it should still be a date field afterward so that I can match it against a date in a database.
At the moment all I'm managing to do is to change it to a string in that format.
I tried this type of code which also did not work.
DateTime.Parse(yourDataAsAString).ToString("yyyy-MM-dd")
fromDeString = String.Format("{0:yyyy/MM/dd}", aDate)
fromDate = Format("{0:yyyy/MM/dd}", aDate)
Any help would be much apreciated, thanks
You're not understanding that a date object does not store the digits in any particular format. The only way to get the digits formatted in the order you want is to convert it to a string. Why do you need to compare them in a particular format? A date is a date no matter how it is formatted. 12/15/78 == 1978/12/15.
If you are not able to compare dates from the DB against a date object in VB, it is likely that the date you are comparing to in the database is being returned to you in string format, in which case you should covert it to a date object for comparison.
Dim sDate As String = "2009/12/15" 'Get the date from the database and store it as a string
Dim dDate As New Date(2009, 12, 15) 'Your date object, set to whatever date you want to compare against
Select Case Date.Compare(dDate, Date.Parse(sDate))
Case 0
'The dates are equal
Case Is > 0
'The date in the database is less
Case Is < 0
'The date in the database is greater
End Select
Here's a sample module demonstrating the functionality you desire.
Imports System.Globalization
Module Module1
Sub Main()
Dim culture As New CultureInfo("en-us", True)
Dim mmDDyy As String = "10/23/2009"
Dim realDate As Date = Date.ParseExact(mmDDyy, "mm/dd/yyyy", culture)
Dim yyMMdd As String = realDate.ToString("yyyy/MM/dd")
End Sub
End Module
Hope this helps.
Kind Regards
Noel
Your second should actually work. Instead just try:
dim chislo as date = date.now dim message As String = $"
Today`s Date: {String.Format("{0:dddd, dd/MM/yyyy}", Chislo)} "
MsgBox(message)