convert utc time into local time using vb.net - datetime

Using vb.net am trying to convert utc time to local time .but i think am getting the wrong result. Here i enclosed mycode.
code
Dim time As DateTime = New DateTime()
time = Date.FromFileTimeUtc(1344502618)
MsgBox(time)
getting result like this
1/1/1601 12:02:14 AM
is this result is correct?

You can just write
Dim time As Double = Convert.ToDouble(1344502618)
Dim baseDate As New Date()
Dim result As Date = baseDate.Add(TimeSpan.FromSeconds(time))
MsgBox(result)

Related

Parsing unknown date fromat from String

Is there a way to programmatically find the Date/Time format of a string in Visual Basic?
My example comes from work. I was working on an Custom Web Page to compare tables on Excel Spreadsheets . My comparison of the records on each table is based off of last four digits of social security and birthdate, however currently the format of the birthdate has to be the same on both spreadsheets for it to run properly. The user wanted to compare early before we had updated the reports for this project, and I told her to make sure the birthday columns were in the same datetime format before she ran the web page. She did not.
I have two options, hard code the date time format that currently exists on the spreadsheets, or find a way to make the format irrelevant. I think it would be more fun to do the latter.
I know that there is a Date.ParseExact method, but that requires that you know the format of the string already.
To give you a better idea of what I want to do:
Dim Birthday1 As String = "04/15/1998 13:00:00"
Dim Birthday2 As String = "04/15/1998 1:00 PM"
'//Converts to Date in format "M/d/yyyy"
Dim dateBirthday1 As Date = ConvertFromAnyDateFormat(Birthday1)
Dim dateBirthday2 As Date = ConvertFromAnyDateFormat(Birthday2)
If dateBirthday1 = dateBirthday2 Then
'//Do comparison operations or whatever
End If
In your case, the strings are in good format. So just allow the system to cast the values
This will work for your two dates.
Imports System.Globalization
Public Class MyFunExample
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Birthday1 As String = "04/15/1998 13:00:00"
Dim Birthday2 As String = "04/15/1998 1:00 PM"
Dim Birthday3 As String = "04/15/98 1:00 PM"
Dim Birthday4 As String = "04/15/98"
'//Converts to Date in format "M/d/yyyy"
Dim dtBir1 As DateTime = Birthday1
Dim dtBir2 As DateTime = Birthday2
Dim dtBir3 As DateTime = MyDateCheck(Birthday3)
Dim dtBir4 As DateTime = MyDateCheck(Birthday4)
Debug.Print(dtBir1)
Debug.Print(dtBir2)
Debug.Print(dtBir3)
Debug.Print(dtBir4)
if dtBir1 = dtBir2 then
End if
End Sub
Function MyDateCheck(sDate As String) As DateTime
If IsDate(sDate) Then
Return sDate
End If
Dim rFoundOne As DateTime
' if we get here, then lets try some other formats
If DateTime.TryParse(sDate, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite, rFoundOne) Then
Return rFoundOne
End If
' try additonal messy cases
If DateTime.TryParseExact(sDate, "mm dd yy", CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite, rFoundOne) Then
Return rFoundOne
End If
' if all fail, then we return a "0" date
Return DateTime.MinValue
End Function
Output:
1998-04-15 1:00:00 PM
1998-04-15 1:00:00 PM
1998-04-15 1:00:00 PM
1998-04-15
So if things are messed up - then you could introduce the date check routine, but in your case you just need to shove the strings into a datetime, and thus they are then considered NOT a string, but a internal date format number, and thus the compares will work.

displaying date in MM/dd/yyyy format using label

im new to asp.net,im trying to display out my date output in terms of
MM/dd/yyyy
but it came out with
MM/dd/yyyy 12:00:00 AM
what can i do to remove the time?
my vb.net code is like this:
Dim date As String = myDataReader("date").ToString()
lbldate.Text = date
You can pass the format to ToString() method.
Dim date As String = myDataReader("date").ToString("MM/dd/yyyy")
You need to pass the format to the .ToString method, like this:
.ToString("MM/dd/yyyy")
It would also be a good idea to cast the myDataReader("date") to a DateTime object, like this:
Dim dateValue As DateTime = CType(myDataReader("date"), DateTime)
Dim date As String
If dateValue Is Not Nothing Then
date = dateValue.ToString("MM/dd/yyyy")
End If
Note - If the TryCast fails, then Nothing would be the value of dateValue, hence the If dateValue Is Not Nothing Then logic is there.

Conversion from string to datetime not being done automatically by CLR

I have to retrieve value of label containing date and compare it with current date. I tried converting its value to date first then formatting, but it is not working. I used CDATE, Convert.datetime and CTYPE. But each one is giving same result. My code is as below:
Dim datevoucher As DateTime = Format(CType(lblDate.Text, DateTime), "yyyy-MM-dd")
I am getting below error :
Conversion from string "05/31/2009" to type 'Date' is not valid
In my case lblDate.text = "05/31/2009"
I also tried as below, but still showing error as " cannot convert string to datetime."
Dim datevoucher As DateTime = Format(DateTime.Parse(lblDate.Text), "yyyy-MM-dd")
I tried also using culture like below, but again not successfull, it worked initially, but then it abruptly started throwing error on it also. I changed nothing at all in code :
Dim dt As DateTime = DateTime.Parse(lblDate.Text,
System.Globalization.CultureInfo.GetCultureInfo("en-GB").DateTimeFormat)
From last two days, I have tried everything I could find on internet, but problem is persisting.
Why this is not working
Dim datevoucher As DateTime = Format(CType(lblDate.Text, DateTime), "yyyy-MM-dd")
because
Format() will produce an output in string format it is not directly cast to the DateTime format. that's why you are getting the error
Text is also cannot be converted to dateTime format.
Here i proposed you [CDate][1] for this conversion
For example :
Dim s As String = "05/31/2009"
Dim mydate As DateTime = CDate(s)
for your scenario:
Dim datevoucher As DateTime = CDate(lblDate.Text)

UTC to datetime conversion

I have the following date time string:
2013-08-16T09:21:05-04:00
How can I get that into a local datetime format in VB.NET?
UPDATE
Just realised the string had some extra quotes on each side that hadn't been sanitized properly #blush
Look at the DateTime.ParseExact() and DateTime.TryParse() methods.
http://msdn.microsoft.com/en-us/library/System.DateTime_methods.aspx
Have you tried something like this
Dim s As String = "2013-08-16T09:21:05-04:00"
Dim d As DateTime = DateTime.Parse(s)
Dim dateValue As DateTime = DateTime.Parse("2013-08-16T09:21:05-04:00")
Debug.WriteLine(dateValue.ToString)
Output on my machine (British English, GMT+1):
16/08/2013 14:21:05

VB.net Date Format Gridview dd/mm/yyyy

Hello
I'm trying to update a ms access database. I've got a gridview with a date that i use:
Dim tBox As TextBox = CType(gridStaff.Rows(e.RowIndex).FindControl("sDate"), TextBox)
I then want to take the value from this textbox and assign it to a date variable.
Dim test As Date = CDate(tBox.Text)
The problem I have is that now when I put test variable into my sql update query it stores the date in this format. mm/dd/yyyy instead of the dd/mm/yyyy format I want.
Ive tried different ways to format it I read online but to no success yet. Any advice would be great!
Thanks
Is your problem the conversion from tBox.Text to Date, or is it at the sql update query level? -- You should probably give the sql statement a parameter with a strict type. Converting from tBox.Text into test should probably be done in a Culture aware manner:
'String to convert and target date:
Dim dateString As String = "02/15/2011"
Dim d As Date
' Specify English/US culture when converting string:
Dim cul As Globalization.CultureInfo = Globalization.CultureInfo.GetCultureInfo("en-US")
d = Date.Parse(dateString, cul)
You could also rely on the default culture and simply let Date.Parse do the job without specifying the culture.
try using
Dim test As Date = DateTime.ParseExact(tBox.Text, "d/M/yyyy", CultureInfo.InvariantCulture)
or if your System is set up right and CultureInfo.CurrentCulture has the right date format,
Dim test As Date = DateTime.Parse(tBox.Text, CultureInfo.CurrentCulture )
instead of CDate()

Resources