Convert AD Date String Value to DateTime in VB.NET - 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

Related

Get the 'YESTERDAY DATE' - 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.

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.

how to convert the string to date in vb.net

I AM FACING ONE PROBLEM.
I AM USING BELOW METHOD FOR MAKING STRING VALUE TO DATE.
string values is like this ="01/12/2002" like "dd/MM/YYYY"
My problem is that.
two string values
->1)01/01/2025
->2)1/1/2025
i am getting the Value like above 1 or 2
Dim d As Date = DateTime.ParseExact(dumm, "dd/MM/yyyy", Nothing)
if 1 comes nothing will happen but
if i get 2 i am facing error like String was not recognized as a valid DateTime.
As per my Analysts what i understood is date should be 2 digits Remaining all are two digits other wise giving the error.
but some times i am getting single digits from the excel to vb.net
how can i solve this issue...
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact(dumm, "d/M/yyyy", Nothing)
i put the break point on dumm ok dumm vlaues= "1/2/2026 12:00:00 Am"
Error...............
now
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact("01/02/2026", "dd/MM/yyyy", Nothing)
Working Fine.......
Use the date format string "d/M/yyyy h:m:s tt". This will handle both cases i.e. with and without leading zero for the day and month. Additionally, since your actual variable has a time component in addition to the date, you need to add the format string for parsing time as well.
However, I would advise you to use TryParseExact, which will return boolean values based on success or failure of the parse rather than throwing exceptions.
Demo for using TryParseExact along with appropriate format string.

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.

Date Format in a textbox

Can someone help me to format this.
in my textbox (The date is 10222012)
the output should be (The date is 10/22/2012)
the text should be still there
thank you
A couple of approaches off the top of my head.
If the text is coming from one variable, for example:
Dim MyText As String = "The date is 10222012"
Then you can use String functions to get the date (assuming it is always 8 characters, which can be a dangerous assumption), format it and stick it back in:
Dim MyDate As String = CType(MyText.Substring(MyText.Length - 8), DateTime).ToString("MM/dd/yyyy")
MyText = MyText.Substring(0, MyText.Length - 8) & MyDate
If on the other hand the date is in a separate variable, say a DateTime variable, you can do this:
Dim MyDateFormatted As String = MyDate.ToString("MM/dd/yyyy")
There are other ways to do this. The key here is to get the date and convert it to the format you want via the DateTime.ToString Method (String) method, where the format string in the parantheses tells what format to use.
EDITED
That's what I get for not trying the code out first...if the string "10222012" isn't recognized as a valid format, there's another way to do this:
Dim MyText As String = dRecord.Rows(i).Item(iCount).ToString
Dim MyDate As String = MyText.Substring(MyText.Length - 8)
MyDate = MyDate.SubString(0, 2) & "/" & MyDate.Substring(2, 2) & "/" & _
MyDate.Substring(4)
oSheet.Cells(x, iCount - 2) = MyText.Substring(0, _
dRecord.Rows(i).Item(iCount).Length - 8) & _
MyDate
It's a bit more brute force and ugly, but that should get you going in the right direction. You might be able to get the same result use Insert (another String operator), but that might be just as ugly.
If you need to do this conversion a lot, I recommend putting it into a helper method, or better yet an extension method.
2nd Edit
You can check for blank record easily enough:
Dim MyText As String = dRecord.Rows(i).Item(iCount).ToString
If Not String.IsNullOrEmpty(MyText) Then
' convert the format here
End If