Get the 'YESTERDAY DATE' - VB.NET - vb.net

Dim tingin As Date = DateAndTime.DateAdd(DateInterval.Day, -1, DateAndTime.Now)
Dim trimmoto As String = Trim(tingin)
MsgBox(trimmoto)
I want to get the 'DATE ONLY'

Add :-
Dim tingin As Date = DateAndTime.DateAdd(DateInterval.Day, -1, DateAndTime.Now)
Dim trimmoto As String = tingin.ToString("yyyy/MM/dd")
MsgBox(trimmoto)
Or :-
Dim tingin As Date = Now.AddDays(-1)
Dim trimmoto As String = tingin.ToString("yyyy/MM/dd")
MsgBox(trimmoto)

I believe this has been already answered.
I just want to emphasize the use of depreciated VB Functions.
Do not use the TRIM() command anymore as this is already depreciated.
This dates back to VB6 and Classic VBSCRIPT.
It is just there for backward compatibility.
Use the .NET counterpart .TOSTRING.TRIM
Same goes with FORMAT(NumberVariable,"yyyy-dd-MM").
Use .TOSTRING.FORMAT(NumberVariable,"yyyy-dd-MM") instead.

Related

Convert AD Date String Value to DateTime in VB.NET

I need to convert Date VALUE retrieved from Active Directory to something readable.
I know external windows System32 application can be used:
CMD - > w32tm.exe /ntte 128271382742968750 gives
148462 05:57:54.2968750 - 6/24/2007 6:57:54 AM
I tried:
Dim returnString as String = "128271382742968750"
Dim dta As DateTime = TimeValue(returnString)
That throws an exception.
Any suggestions welcome!
Active Directory stores these values as Windows file times, which you can convert using DateTime.FromFileTime():
Dim returnString as String = "128271382742968750"
Dim dta As DateTime = DateTime.FromFileTime(Long.Parse(returnString))
Result:
6/24/2007 5:57:54 AM
See it here:
https://dotnetfiddle.net/wH2Re0

convert Integers to Date Objects VB

I have 2 integers and 1 String. Now I want to convert it in date objects and display it in datepicker1 (I set its format to "short")
for example:
Dim Day As Integer = 14
Dim Month As String = "July"
Dim Year As Integer = 2016
I am expecting an answer as
7/14/2016 'this must be displayed in datepicker1
I searched through the internet and i found the same question here but this is from different language and i want it in VB. if my question has already been asked before. please post the link.
I am new to vb so i am hoping for your help guys!
Here's a way that uses Date.ParseExact:
Dim dt = Date.ParseExact($"{Year}-{month}-{day}", "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Note that i'm using string interpolation at $"..." which is available in VS 2015/VB14. Otherwise you could use String.Format:
Dim dtStr = String.Format("{0}-{1}-{2}", Year, month, day)
Dim dt = Date.ParseExact(dtStr, "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Here you'll find more informations about the custom date and time format strings.

String was not recognize as a Valid datetime in vb.net

I'm trying to highlight all the rows in the datagridview which date is more than five days from the date today.
Here is my code.
For Each row As DataGridViewRow In dgv_Transfer_ledger.Rows
Dim now As DateTime = Date.Now
Dim delayDate = Date.Parse(row.Cells(0).Value.ToString)
Dim fiveDaysbefore = delayDate.AddDays(-5)
If now > fiveDaysbefore AndAlso now < delayDate Then
row.DefaultCellStyle.BackColor = Color.Yellow
ElseIf now > delayDate Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
I'm getting the error String was not recognized as a valid DateTime.
Why I'm getting this error? Can anyone help me please? Thanks in Advance!
is your date in cell showing the correct format?
as they said, you need to check your cell's data and make sure it is in the correct format before they can actually run your code
if you know the format you may try
EG. (DD/MM/YY) (MM/DD/YY) [or something like this]
Dim delayDate = Date.ParseExact
I already got the answer.
Dim currentdate As Date = Date.Now.ToString("d")
You have to format the date based on the datatype in your datagridview.
I didn't use parse or ParseExact for getting the delaydate. Just make sure that the cell format in datagridview is the same format of the current date :)
Thanks for the reference guys :)

Excel VBA - Month in "mmmm" format

I need to use the current and the previous month in "mmmm" format, and the best solution I found is MonthName(Month(Date)).
However, Excel keeps giving me:
Compile Error: Object Required
And it's driving me crazy. I'm pretty new to VBA so can anyone explain what I am doing wrong here?
Sub test()
Dim CurrentMonth As Variant
Dim PreviousMonth As Variant
Set CurrentMonth = MonthName(Month(SetupSheet.Range("C9"))) '<-------HERE
Set PreviousMonth = MonthName(Month(SetupSheet.Range("C9") - 1))
...
End Sub
You don't need to use Set. You only use that if you are passing Object to a variable.
This line returns a String and not an Object (e.g. Range, Worksheet, Chart, etc.)
MonthName(Month(SetupSheet.Range("C9"))) 'if C9 contains a valid date format
So to pass it to CurrentMonth variable, simply assign it using = sign:
CurrentMonth = MonthName(Month(SetupSheet.Range("C9")))
Lowercase 'm' is minute. Uppercase 'M' is for month.

Difference between CType(str,DateTime) and DateTime.Parse(str)

I want to convert str into DateTime.
For that which option should I used in VB.NET? And Why?
I tend to do it like this:
Dates "disguised" as Object
If I know the object is a datetime I use CType:
Dim table As New DataTable("Table")
table.Columns.Add("DATETIME_COLUMN", GetType(DateTime))
table.Rows.Add(Date.Now)
table.AcceptChanges()
Dim d As DateTime = CType(table.Rows(0).Item("DATETIME_COLUMN"), DateTime)
Strings
When dealing with strings I use DateTime.Parse. Notice that you can pass a cultureinfo.
Dim d As DateTime = DateTime.Parse("10.02.2014", New System.Globalization.CultureInfo("nb-NO"))
Unknown/mixed types
Finally, if I cannot be sure of the datatype, I use Convert.ToDateTime:
Dim d As DateTime = Convert.ToDateTime(obj, New System.Globalization.CultureInfo("nb-NO"))
CType & CDate are VB.NET functions that have defined functionality.
Ctype('01/01/2014',DateTime)
DateTime.Parse is a BCL (Base class library) method that has defined
functionality.
Dim value As String = "2000-02-02"
Dim dt As DateTime = DateTime.Parse(value)
As long as your string is in the right date format, you can just use CDate() function. This is the fastest and easiest way since you only have to type 7 characters excluding the string to be converted :) In terms of efficiency and speed, doesn't really make a difference.