|
-
Aug 22nd, 2008, 08:44 PM
#1
Thread Starter
Addicted Member
date convertion
hi gurus
I need some help, I have a text file in which there is a date value, this value comes in the following format yyyymmdd, I have a field waiting for this value which is a date type, I need to convert this string into date,the date format I need is MM/dd/yyyy, I used the date.parseExact but this gave me the following m/d/yyyy and I need MM/dd/yyyy(e.i 19840906 comes out using date.parseExact as 9/6/1984) and I need this to be 09/06/1984.
Thanks for any help
-
Aug 22nd, 2008, 09:06 PM
#2
Re: date convertion
OK, hold up a minute. If you used Date.ParseExact then you did NOT get any format at all. Date.ParseExact creates a binary Date object and such objects have no format at all. Format is ONLY an issue when DISPLAYING a date, which means when creating a string representation. If you need to use a Date value then you do NOT need to concern yourself with format at all.
Consider this: I have the number 100. If I add the number 10 to that, what's the result? It's 110, right? Now, if I was to start with 100 again and add the number 010, what would be the result? It would still be 110, right? The fact that I REPRESENT the number ten with a leading zero or not doesn't change the value of the number.
Now, consider this. There are three friends and they were born on 6-Sep-1984, 9/6/1984 and 09/06/1984. Were they all born on the same day? Of course they were. The fact that I REPRESENT their birthdays using different formats doesn't change the actual date.
So, if you do this:
vb.net Code:
Dim str As String = "19840906" Dim dt As Date = Date.ParseExact(str, "yyyyMMdd", Nothing)
then you have a Date object that represents the sixth day of September in 1984. It has no format. Internally it's just a number. If you need to use a Date in a calculation, store it in a database or whatever, that is the value you use. Now, if you want to DISPLAY that date to the user in a specific format then you must convert it to a String first. It's at that point of conversion that you decide what format you want to use. You could do this:
vb.net Code:
MessageBox.Show(dt.ToString("M/d/yyyy"))
or you could do this:
vb.net Code:
MessageBox.Show(dt.ToString("MM/dd/yyyy"))
or you could use any one of a zillion other standard or custom formats. It's only an issue when you want to display a string representation though.
I hope that this has reinforced that Dates and string representations of dates are NOT the same thing. Format is ONLY an issue when dealing strings and you should NEVER convert a binary Date object to a string unless it is for display or serialisation purposes. An example of serialisation is to store in an XML file. In ALL other cases you work with binary Date objects exclusively.
-
Aug 22nd, 2008, 10:10 PM
#3
Thread Starter
Addicted Member
Re: date convertion
J.
when I used the word format , I was trying to represent the way the string was coming in, did not mean to use it to represent display. I undersatnd you point and thanks for the free tutorial, much much clear now on this regard.
Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|