Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Convert string 19990120 to date in vb.net

Hi experts.
I need to convert a  string that looks like 19990120 to date in vb.net.
I know I can pull the st ring apart and add  / to it, but is there a convert or cast that will do the same?
thanks
0
shayne23d
Asked:
shayne23d
  • 3
  • 3
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
Hi shayne23d,

try this:
 DateTime.ParseExact(pDate, "yyyyMMdd", Nothing)

Cheers!
0
 
broadbentCommented:
The format above would be
Dim a as string = cstr(19990120)
Dim d as new date(a.substring(0,4),a.substring(4,2),a.substring(6.2))
0
 
shayne23dAuthor Commented:
ok so this works,
        Dim a As String = CStr(19990120)
        Dim d As New Date(a.Substring(0, 4), a.Substring(4, 2), a.Substring(6.2))


but this does not work
        Dim a As String = CStr(19990120)
        Dim d As New Date
 d = (a.Substring(0, 4), a.Substring(4, 2), a.Substring(6.2))
what if I already have the date


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
shayne23dAuthor Commented:
I meant to say  ok so this works,
        Dim a As String = CStr(19990120)
        Dim d As New Date(a.Substring(0, 4), a.Substring(4, 2), a.Substring(6.2))


but this does not work
        Dim a As String = CStr(19990120)
        Dim d As New Date
 d = (a.Substring(0, 4), a.Substring(4, 2), a.Substring(6.2))
what if I already have the date varaible declared?
0
 
Éric MoreauSenior .Net ConsultantCommented:
no.

(a.Substring(0, 4), a.Substring(4, 2), a.Substring(6.2)) are the parameters to the Date contructor.
0
 
shayne23dAuthor Commented:
Ok so If I am filling a dataset from a fixed lenght txt document with a thousand recoreds in it I need to  use the date constructor on each line as I loop through?
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>I need to  use the date constructor on each line as I loop through?

yes

Have you tried the ParseExact method I gave you?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now