Getting the day of a date - vb.net

I have a series of dates formatted as:
26/03/1992
12/06/2010
13/01/1995
etc... it's in DD/MM/YYYY. I need to find the day like "Tuesday", "Monday" etc out of them.
I know I need to parse the date or something but I'm unsure how to go about this.

You can cast it as a DateTime and use the DayOfWeek property which returns a DayOfWeek enumerator.
Not sure in VB.NET but in C# it's like
DateTime.Now.DayOfWeek or DateTime.Parse(theDateString).DayOfWeek

You want to look at the format strings for the ToString method. MyDate.ToString("dddd") will get you what you want.

The best thing it works for me in vb 2010 is
I add timer and enabled it then i put it like that
Label6.Text = Format(Now, "dddd/dd")
"dd" gives me the day Num.
"dddd" gives me the name of the day

DateTime.Parse("2010/12/31").dayofweek

Convert the date into a date datatype, then use the format function. This displays monday:
Dim d As Date
d = "11/23/2009"
MsgBox(Format(d, "dddd"))
You could also get a numeric day of the week using d.DayOfWeek.

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ArgumentOutOfRangeException
Inherits ArgumentException
Implements ISerializable
'Usage
Dim instance As ArgumentOutOfRangeException

Have a look at the documentation of the DateTime members Parse, TryParse, DayOfWeek and ToString.

Try
dateValue.DayOfWeek.ToString()

In VB try this:
DateTimePicker1.Value.DayOfWeek.ToString
It works

MsgBox(Now.Date.DayOfWeek.ToString)

Related

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)

Not able to get month from current date

Am trying to extract month from the current date but in vain. I am using the code:
Format(Today.Date, "mmmm")
However when I try to run it to display month like January it instead displays 00. I thought that this would work but it isn't. What can I do to get the month from current date in vb.net using a simple approach like a single function?
Try This :
DateTime.Today.ToString("MMMM")
Check out This for detailed date formatting options.
try this
MonthName(Now.Date.Month(),true)
msdn: Custom Date and Time Format Strings
Format(Today.Date, "MMMM")
EXTRACT(MONTH FROM NOW()) , getting month of current date

exclude weekends from datetime

I need to exclude weekends from this line.
If Convert.ToDateTime(e.Row.Cells(4).Text).ToString < DateTime.Now.AddDays(-3) Then
e.Row.BackColor = System.Drawing.Color.FromName("red")
I hope there is an easy way to do this!
Use the following code
DateDiff(DateInterval.Weekday,Date2, Date1)
--Hiren V
I've had to do something similar once.
The trick is to take the strings from your rows, convert them to a DateTime like you have it, then check the DayOfWeek property of the DateTime object.
The DayOfWeek property can be used as such:
DateTime date = new DateTime();
label1.text = date.DayOfWeek.ToString();
With that, just check if the value is equal to a string of "Saturday" or "Sunday".
That should help you filter off your weekends.

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))

VB.NET Get Only Year from Date

I'm pulling into a variable a datetime value. Now, I want to post this value back to my database, but I need it to be only the year digits. How do I get VB.NET to trim the month, day, and time off and just return the four character year (e.g. 2011)?
Date.Today.Year is what you're looking for, or for an existing date, just someDateVariable.Year
i know this is a bit late to answer
but, it wont hurt telling, try "year(now) " and load it to a variable
Example:
Dim Year_in_Digits = Year(Now)
Dim myDate As DateTime = #1/1/2011#
Dim myYear As Int32 = myDate.Year
There is a property on the DateTime structure called 'Year'. This returns an integer representing just the year.
If you need to convert this to a string, just use the ToString() function.
So..
MyDT.Year.ToString()
That's a c# example, I'm sure VB.Net is going to be very similar.
I just had to do this for my VB program.
Dim Year As Integer
Year = Convert.ToInt32(Now.ToString("yyyy"))
Use just "yy" if you want two digit year. Then, when you need to display it somewhere:
year.tostring
I hope this will help because I tested it
Dim nowYear As Integer = Date.Now.Year
I know it's late,but am sure i will help someone
To get exact year in vb:
Dim date As String=Now.Year.ToString()