exclude weekends from datetime - vb.net

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.

Related

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

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 do I grab this integer from a date in VB?

txtDate = 3/7/1994
Basically I want my button to calculate the 'month' digit (in this case 7) and display it into txtMonth.
What is the simplest way to do this?
Note the date will come from a user's input.
By the way, it's for Visual Basic! If you could actually explain it instead of telling me what to do, that would be great!
Found the code:
Dim theDate As Date
Dim theMonth As Integer
theDate = txtDateOfBirth.Text
theMonth = Month(theDate)
txtMonth.Text = theMonth
Cheers!
Split 3/7/1994 it on slashes, pick the second value from the splits and then assign it to txtMonth. This should be doable in whatever your programming language is :)
In C#
string[] splits = dateValue.Split("/".ToCharArray());
txtMonth = splits[1];
Supposing dateValue = "3/7/1994"
But do specify your programming language.
Split the string into an array (on "/"), the second item (1), will be your month.
convert your date variable to a datetime then use the format of datetime (C# code):
DateTime dt;
bool isValid = DateTime.TryParse(txtDate, out dt);
if (isValid)
dt.ToString("MM");

VB.Net 2005, how can I subtract a date from the (current date minus 7 days) and produce a number of days?

I have a date in the future e.g. 13/10/2008 I need to subtract the current date (today is the 28/09/2010) minus 7 days, so thats 21/09/2010 minus 13/10/2008, which would equal erm, 720 something ?
But the current date won't always be 28/09/2010, obviously.
I need the code for this.
EDIT: When i said future I mean past :)
Sub Main()
Dim dt As DateTime = New DateTime(2008, 10, 13)
' be careful what you are subtracting from what
' the date you have is not in the future (year 2008)
' if the date is in the future: (dt.Subtract(DateTime.Now.AddDays(-7))).TotalDays
' or simply take the absolute value
Dim days As Double = (DateTime.Now.AddDays(-7).Subtract(dt)).TotalDays
Console.WriteLine(days)
End Sub
You will also notice that the TotalDays property is of type Double.
13/10/2008 is not exactly in the future :)
Sorry for using C# code, but:
(dateInFuture - DateTime.Now.AddDays(-7)).TotalDays
Should work. Of course the other way around if you mean in the past:
(DateTime.Now.AddDays(-7) - dateInPast).TotalDays
Dim ValidDate As Date =cDate("Tuesday, December 31, 2013") 'A date in Future
Dim date1 As New System.DateTime(ValidDate.Year, ValidDate.Month, ValidDate.Day)
Dim date2 = Now
Dim Diff1 As System.TimeSpan
Diff1 = date1.Subtract(date2)
Dim TotRemDays = (Int(Diff1.TotalDays))
MsgBox(TotRemDays)
"I need the code for this" seems a bit too much like "Plz give meh teh codez", and your "date in the future" seems a little bit in the past.
Anyway, you should investigate the relevant methods of the DateTime structure, in particular the Subtract method (both overloads, or in alternative its subtraction operator), and you should have a look at the TimeSpan structure too.
You could create a DateTime for the date of today, subtract a TimeSpan of 7 days to it, and then subtract such result to a DateTime representing your date in the future (or, if it is in the past, do the opposite). You'll get a TimeSpan representing the difference in time between the two dates, from which you can easily get the number of days using its Days property.
As other said, to do the first subtraction you can also use the AddDays method of the DateTime structure.

Getting the day of a date

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)