Answered by:
Date Formatting

Question
-
User-280752524 posted
Hi Guys,
Need you idea on this one.. I have a textbox then i need to convert the text that the user input in that text box into date..
For example the user input :1/2/2005 then i need to convert this into : 01/02/2005
anyone knows how to convert this.
I have this code but unfortunatley the conversation to the format that i need is wrong..Dim podate As Date
podate = Format(CDate(txtdate.Text.ToString), "MM/dd/yy")
It only gives me : 1/2/2005
I need is : 01/02/2005
Pls help me.
Thanks and Regards.
AnnirTuesday, March 23, 2010 10:43 PM
Answers
-
User-513735053 posted
podate = CDate(txtdate.Text.ToString("mm/dd/yyyy"))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 26, 2010 2:44 AM -
User-952121411 posted
Here is the code you need:
'Create a variable to hold a date representing an improperly formatted date (for this example) Dim MyDate1 As String = "1/2/2005" Dim MyStringDateFormatted As String = Format(Date.Parse(MyDate1), "MM/dd/yyyy")
... the output is as follows:?MyStringDateFormatted
"01/02/2005"...and for your example directly, try the following:
Dim MyStringDateFormatted As String = Format(Date.Parse(txtdate.Text.ToString), "MM/dd/yyyy")
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 26, 2010 4:36 PM
All replies
-
User-1636183269 posted
Dim podate As Date
podate = Format(CDate(txtdate.Text.ToString("00/00/2000")), "MM/dd/yy")I have not tested but sure it will work
Tuesday, March 23, 2010 11:35 PM -
User-280752524 posted
Hi,
Thanks for the quick reply. I try your code but it gives me an error:
unable to cast object of type 'System.String' to type 'System.IFormatProvide'
Thanks.
Annir
Wednesday, March 24, 2010 12:50 AM -
User1485408165 posted
To get the 0's in your output strings try:
Dim culture = CultureInfo.CurrentCulture
Date.ParseExact(txtdate.Text, "mm/dd/yyyy", culture).ToString("mm/dd/yyyy")
Thursday, March 25, 2010 6:42 PM -
User-280752524 posted
Hi,
Thanks for the reply, I try to use this but it gives me an error at Dim culture = CultureInfo.CurrentCulture saying that the CultureInfo is not yet declared.. Is culture mas be declared as date? string? or ??
Thanks and Regards.
annirFriday, March 26, 2010 2:11 AM -
User-513735053 posted
podate = CDate(txtdate.Text.ToString("mm/dd/yyyy"))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 26, 2010 2:44 AM -
User-952121411 posted
Here is the code you need:
'Create a variable to hold a date representing an improperly formatted date (for this example) Dim MyDate1 As String = "1/2/2005" Dim MyStringDateFormatted As String = Format(Date.Parse(MyDate1), "MM/dd/yyyy")
... the output is as follows:?MyStringDateFormatted
"01/02/2005"...and for your example directly, try the following:
Dim MyStringDateFormatted As String = Format(Date.Parse(txtdate.Text.ToString), "MM/dd/yyyy")
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 26, 2010 4:36 PM