UTC to datetime conversion - asp.net

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

Related

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)

Converting Date From TextBox To Date In ASP.Net

I want to get the difference of date from Two TextBox say to and from dates..
My Code is here
Dim ts As TimeSpan
ts = CDate(txtTo.Text) - CDate(txtFrom.Text)
txtDays.Text = ts.Days() + 1
but this code is throwing an error like this
Conversion from string "16/11/2011" to type 'Date' is not valid.
In general, try to avoid using old VB functions like CDate. In this case, you should use the Parse method:
Dim ts As TimeSpan
ts = DateTime.Parse(txtTo.Text) - DateTime.Parse(txtFrom.Text)
Or, if you know the date format in advance:
Dim fmt As String = "dd/MM/yyyy"
Dim ts As TimeSpan
ts = DateTime.ParseExact(txtTo.Text, fmt, Nothing) - DateTime.ParseExact(txtFrom.Text, fmt, Nothing)
Try using ParseExact like:
Dim s As String = txtFrom.Text.trim
Dim pattern As String = "dd/MM/yyyy"
Dim parsedDate As Date = Date.ParseExact(s, pattern, Nothing)
As that way you can specify the format of the string that is used.
More direct to your example:
Dim pattern As String = "dd/MM/yyyy"
ts = Date.ParseExact(txtTo.Text, pattern, Nothing) - Date.ParseExact(txtFrom.Text, pattern, Nothing)
You can directly cast a string to DateTime. You need to use the DateTime.Parse function to parse a string to a DateTime.
The syntax can be found here.
Use overloaded version of the function:
DateTime value = DateTime.Parse(txtFrom.Text, new CultureInfo("gu-IN", false));
where "gu-IN" is for Gujarati (India)

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

String / DateTime Conversion problem (asp.net vb)

I have this code:
Dim birthdaystring As String = MonthBirth.SelectedValue.ToString & "/" & DayBirth.SelectedValue.ToString & "/" & YearBirth.SelectedValue.ToString
Dim birthday As DateTime = Convert.ToDateTime(birthdaystring)
Which produces errors (String was not recognized as a valid DateTime.)
The string was "01/31/1963".
Any assistance would be appreciated.
Thanks.
The problem is probably that the culture used for parsing the date expects a MM/dd/yyyy format rather than dd/MM/yyyy format.
Instead of creating a string and parsing it, create the DateTime value directly:
Dim birthday As New DateTime(YearBirth.SelectedValue, MonthBirth.SelectedValue, DayBirth.SelectedValue)
Instead of creating a string which you later try to parse to a date try this:
Dim birthday As DateTime = new DateTime(_
CType(YearBirth.SelectedValue, Integer), _
CType(MonthBirth.SelectedValue, Integer), _
CType(DayBirth.SelectedValue, Integer))
Try changing it to:
DateTime.Parse(birthdaystring);
I'm guessing it will work, but if not - you can add a second parameter to the parse saying the format you input, i.e.:
DateTime.Parse(birthdaystring,"MM/dd/yyyy");

Resources