Answered by:
Converting a string into required date format in vb.net

Question
-
Hi friends,
I want help in converting a string which contaions 5 numbers,into a required date format for the code below:
dim strDate as string
dim objdate as Date
strDate = "60727" //6 as yy 07 as mm & 27 as dd
strDate = strDate.Substring(0, 1) & "-" & strDate.Substring(1, 2) & "-" & strDate.Substring(3, 2)
objDate = Date.Parse(strDate)
while executing it returns 6/07/2027 which means 27 as year while i want 27 to be a date.
and format should be exactly 2006-sep-27.So plz help me regarding this code.
Monday, October 9, 2006 5:32 AM
Answers
-
Try this:
Public Shared Function ConvertDate(ByVal strdate As String) As DateTime
If strdate.Length = 5 Then strdate = "0" + strdate
If strdate.Length <> 6 Then Throw New FormatException("Invalid date format")
Return DateTime.ParseExact(strdate, "yyMMdd", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
End FunctionMonday, October 9, 2006 9:46 AM -
You can also do this:
Module
Module1 Sub Main() Dim strDate As String = "60727" Dim d As Date = New Date(2000 + strDate.Substring(0, 1), strDate.Substring(1, 2), strDate.Substring(3, 2))Console.WriteLine(d)
End SubEnd
ModuleOf course, this assumes that particular format.
Monday, October 9, 2006 7:54 PM
All replies
-
the formatting is done when you call the ToString() of the date object, giving it the format type you want it back in. Example:
objDate.ToString("yyyy-mmm-dd")
whic should bring it back in 2006-sep-27 for example
However your code does appear to be incorrect. you should be having the date string in dd-mm-yyyy format
you should really give the string the full year number also than just a shortened version. Try expermenting and see what happens :-)
- Proposed as answer by Leo Zuo Monday, September 24, 2012 5:38 AM
Monday, October 9, 2006 9:25 AM -
Try this:
Public Shared Function ConvertDate(ByVal strdate As String) As DateTime
If strdate.Length = 5 Then strdate = "0" + strdate
If strdate.Length <> 6 Then Throw New FormatException("Invalid date format")
Return DateTime.ParseExact(strdate, "yyMMdd", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
End FunctionMonday, October 9, 2006 9:46 AM -
You can also do this:
Module
Module1 Sub Main() Dim strDate As String = "60727" Dim d As Date = New Date(2000 + strDate.Substring(0, 1), strDate.Substring(1, 2), strDate.Substring(3, 2))Console.WriteLine(d)
End SubEnd
ModuleOf course, this assumes that particular format.
Monday, October 9, 2006 7:54 PM -
Thanks to all for replying i will try one of these & will write back to forum
regards
Abhishek
Thursday, October 12, 2006 1:27 PM -
It's a bit disturbing to me that a moderator (and, I think, an MS employee on the QA team?) would recommend this code!
First of all, why not present it as a ready-to-use function?
But much more important than that, the code WILL NOT EVEN COMPILE if the person correctly has specified
OPTION STRICT ON
And anyone who programs in VB.NET without using OPTION STRICT ON is just crazy, in my never overly humble opinion. That fact that OPTION STRICT ON is not the *default* was perhaps the worst decision the VB.NET team ever made. Yes, it often means you have to be more correct in writing your code. But it also means that 80% or so of your silly coding errors will be caught at COMPILE time, instead of having them crop up at run-time. The very first thing I tell *ANY* VB.NET user to do when something isn't working is to add OPTION STRICT ON to their project. And AT LEAST 75% of the time this means that they will immediately find their coding error!
So...
That code SHOULD have been written as:
Public Function GetDateYMMDD( ymmdd As String ) As Date
Return New Date(2000 + CINT(ymmdd.Substring(0, 1)), _
CINT(ymmdd.Substring(1, 2)), _
CINT(ymmdd.Substring(3, 2)))
End Function
Without the explicit casts of the strings to integers, you would get three compile errors with OPTION STRICT ON.
Though it would have been even better to use System.DateTime in place of the VB-only Date class/structure. There's no real difference in the code and now your function could be used by C# if it were part of a library. (I'm not sure on this point: possibly the VB class "Date" is converted under the covers to System.DateTime, anyway, in which case strike this paragraph.)
Monday, May 26, 2008 3:39 AM -
I vote for OPTION EXPLICIT but not for OPTION STRICT. It would remove the VB from VB.net
Microsoft MVP J# 2004-2007, Borland Spirit of Delphi 2001Friday, October 24, 2008 3:26 PM -
'vb2008 'format yMMdd Dim strDate As String = "60727" strDate = strDate.PadLeft(6, "0"c) 'make sure length is 6 strDate = strDate.Insert(strDate.Length, strDate.Substring(0, 2)).Remove(0, 2) 'move year to end strDate = strDate.Insert(4, "/").Insert(2, "/") 'insert /'s Dim someDate As Date Date.TryParse(strDate, someDate) 'convert to date
I vote for both
Option Strict On
Option Explicit On
the B in BASIC has been gone for some time.
Friday, October 24, 2008 4:54 PM