How to format time string in VB.NET to AM/PM? - asp.net

I am trying to format a date string. My function currently generates "16:00". I want it to generate "4:00 PM". Here is my code:
Dim eventTime As String
eventTime = Trim(odbcReader("StartTime").ToString)
I tried this:
eventTime = Trim(odbcReader("StartTime").ToString("hh:mm tt"))
But it returns nothing. How do I get the date string to format properly?

You can't format a String as a String. If you want to format a time then you have to start with a time, i.e. a TimeSpan value or a DateTime value. You need to convert your existing String to one of those types, then convert that to a String in the desired format.
As you want a time of day, you need to create a DateTime. A TimeSpan just represents a period of time without reference to a start or end point, so it can't be directly formatted the way you want. You would need something like this:
Dim s1 = "16:00"
Dim t = DateTime.Parse(s1)
Dim s2 = t.ToString("h:mm tt")
Note that the format specifier is "h:mm tt", "hh:mm tt". You specifically said that you expect a result without a leading zero so you wouldn't provide a format specifier that would create a leading zero.

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)

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

Date or DateTime Object to Be independent of the Page Culture

Dim strTime as String = FomatDateForSave("28/12/2010")
Public Shared Function FormatDateForSave(ByVal strDate As String) As Date
FormatDateForSave = Date.ParseExact(strDate,'dd/MM/yyyy', System.Globalization.CultureInfo.InvariantCulture)
End Function
I am expecting strTime to be "12/28/2010" .... But its getting converted to "28/12/2010" ....
The thing is when the operation is performed by FormatDateForSave ... it converts it to "12/28/2010" ...
But when it is returned it is again converted to "12/28/2010"
I have set the Date for Page.Culture to be "dd/mm/yyyy" and want the value to be "mm/dd/yyyy" to be saved in DB.
You should store the date in the db as its native type, not a string. When you fetch it you should format it then.
Why don't you convert your date in sql before insert/update operation?
e.g.
SELECT convert(varchar, getdate(), 101) will give you date in mm/dd/yyyy format.
Sql Convert

Resources