having an issue with date formats

N

Newbie

Hi,

I have a date in a flat file in the format of DDMMYY (280507) that I want
to insert into a database.

I have left/right and sub'd string to get the values of the day, month and
year into 3 string vars (strDay, strMonth, strYear)

Now I want to put the string back together, and concat the three strings so
its now dd/mm/yy

I then when I do a DateTime.Parse(strDate) it uses my local machine settings
(set to MMDDYY) and this then results in an invalid dataformat.

I also need to insert this into a SQL database.

Whats the best way to deal with this problem ???
 
A

Armin Zingler

Newbie said:
Hi,

I have a date in a flat file in the format of DDMMYY (280507) that I
want to insert into a database.

I have left/right and sub'd string to get the values of the day,
month and year into 3 string vars (strDay, strMonth, strYear)

Now I want to put the string back together, and concat the three
strings so its now dd/mm/yy

I then when I do a DateTime.Parse(strDate) it uses my local machine
settings (set to MMDDYY) and this then results in an invalid
dataformat.
I also need to insert this into a SQL database.

Whats the best way to deal with this problem ???

Use DateTime.ParseExact to converte the original string ("280507") into a
DateTime value.

Use the SqlCommand's Parameters property to pass the DateTime value
to the SQL command, so you don't have to care about the format at all.


Armin
 
K

kimiraikkonen

Hi,

I have a date in a flat file in the format of DDMMYY (280507)  that I want
to insert into a database.

I have left/right and sub'd string to get the values of the day, month and
year into 3 string vars (strDay, strMonth, strYear)

Now I want to put the string back together, and concat the three strings so
its now dd/mm/yy

I then when I do a DateTime.Parse(strDate) it uses my local machine settings
(set to MMDDYY) and this then results in an invalid dataformat.

I also need to insert this into a SQL database.

Whats the best way to deal with this problem ???

Hi,
If you're wanting your date-containing string concatenated, you can do
it manually as you've done by using substring.

For example:

Dim rawDate As String = "280507"
Dim strDay As String = rawDate.Substring(0, 2)
Dim strMonth As String = rawDate.Substring(2, 2)
Dim strYear As String = rawDate.Substring(4, 2)

' Declare resulted string
Dim result As String
' Concat day,month,year
result = String.Concat(strDay, strMonth, strYear)

In the code above, there's no any usage of any DateTime object to deal
with localization issues. You're just retrieving the string, then
concatenating it. Once you've concatenated in the same way you wish
(can be added "/" slash based on your wish), you're ready to insert or
get it from database.

Hope it gives some idea,

Onur Güzel
(e-mail address removed)
(e-mail address removed)
 
G

Göran Andersson

Newbie said:
Hi,

I have a date in a flat file in the format of DDMMYY (280507) that I
want to insert into a database.

I have left/right and sub'd string to get the values of the day, month
and year into 3 string vars (strDay, strMonth, strYear)

Parse it using ParseExact instead:

DateTime d = DateTime.ParseExact(theString, "ddMMyy",
CultureInfo.InvariantCulture);
Now I want to put the string back together, and concat the three strings
so its now dd/mm/yy

Format the DateTime value:

string f = d.ToString("dd'/'MM'/'yy");
I then when I do a DateTime.Parse(strDate) it uses my local machine
settings (set to MMDDYY) and this then results in an invalid dataformat.

Again, ParseExact:

DateTime d2 = DateTime.ParseExact(f, "dd'/'MM'/'yy",
CultureInfo.InvariantCulture);
I also need to insert this into a SQL database.

Whats the best way to deal with this problem ???

Add it as a parameter to the command and use in the query:

SqlCommand cmd = new SlqCommand("insert into SomeTable (ADate) values
(@ADate)", connection);
cmd.Parameters.Add("@ADate", SqlDbType.DateTime).Value = d;
 
M

Michel Posseth [MCP]

There are a lot of solutions to solve your problem , personly i always go
for the ISO 8601 aproach when dealing with date or date time values that
originate from a string , ISO 8601 will always work on anny localozation and
on anny database ( even Access supports it )


ISO 8601 = YYYY-MM-DD so parsing "2008-06-07" will give you always 7
june 2008 and not 7 june 2008 on one localization and 6 july on another
localization


Your textfile is a contstant DDMMYY so read the date as a string parse it
to ISO 8601 you can then even throw it in its raw form to the database
server as it will be converted on the fly to a correct date format ( as ISO
8601 is a standard ) but you could also first read it in a date time
variabel and then do some stuff with it

hth

Michel
 
A

Armin Zingler

Michel said:
There are a lot of solutions to solve your problem , personly i
always go for the ISO 8601 aproach when dealing with date or date

I don't see why this should help more. I wrote everything necessary to solve
the problem.


Armin
 
M

Michel Posseth [MCP]

Armin ,

I mentioned ISO 8601 for the simple fact that it will work on anny platform
in any situation
and it just is a very handy way of dealing with date / datetime variabels .

Is this group changed to an answering contest for who gives the first answer
, or are we still professionals here who give there view and opinion to a
problem
for wich the TS can choose the for him best solution ? .

regards

Michel Posseth
 
A

Armin Zingler

Michel said:
Armin ,

I mentioned ISO 8601 for the simple fact that it will work on anny
platform in any situation
and it just is a very handy way of dealing with date / datetime
variabels .
Is this group changed to an answering contest for who gives the first
answer , or are we still professionals here who give there view and
opinion to a problem
for wich the TS can choose the for him best solution ? .

No, but considering the fact that the problem has already been solved and
your contribution gives zero additional value to solve the problem, is the
group just used to increase one's posting counter?

I don't see in what way ISO 8601 helps in parsing a string in format DDMMYY,
nor I think that it is a good suggestion not to use a Sql/OleDBCommand's
Parameters property in order to use variable parameters.

Therefore I can only see that you were ignoring more helpful answers.
Otherwise I would have never replied to your posting.


Armin
 
M

Michel Posseth [MCP]

Armin Zingler said:
No, but considering the fact that the problem has already been solved and
your contribution gives zero additional value to solve the problem, is the
group just used to increase one's posting counter?

I don't see in what way ISO 8601 helps in parsing a string in format
DDMMYY,
nor I think that it is a good suggestion not to use a Sql/OleDBCommand's
Parameters property in order to use variable parameters.

Therefore I can only see that you were ignoring more helpful answers.
Otherwise I would have never replied to your posting.


Armin

Armin ,

You showed an option wich could be perfectly valid and i showed another one
that could be perfectly valid
what is wrong with that ? .

Just the fact that you cannot see that there are situations where ISO 8601
can be handy , You and for a fact nor i know the complete project of the OP
so my posting could shine some other light on his problem .
Also i think it is good to show more then one availlable option , just maybe
this person is a coder who CAN think out of the box .

About the rest of your insults , i wonder in what paralel universe you live
cause it isn`t mine perspective nor did i state anything as you claim i did

But okay ,,, i am getting a bit angry right now because it is saturday
evening past 21:00 and i have to defend myself against a person who is
atacking me because i tried to help someone and share some of my views
towards a problem . And before i forget to mention it Why ???? for
absolutely nothing nada, zero, nichts ! , niks , noppes , just because the
comunity ( especially the DEVX groups ) has given me so manny in the past
i feel it as my duty to give something back it might be hard to believe for
you but it is the sole truth .

It are the personal attacks by sociopaths that seem so common nowadays in
the groups that make me wonder if it isn`t time for me to say enough is
enough !
and just quit this almost daily ritual to see if i can contribute to a
thread , or even learn something new from my peers . but it looks like the
people who are Unable or unwilling to conform to normal standards of social
behavior are raging these groups nowadays i hope you are not turning in to
one of them cause i valued you much higher .

regards

Michel Posseth
 
A

Armin Zingler

I forgot that criticism is often considered insulting in international
groups. It wasn't ment that way. So, calm down and don't make a big issue
out of it, Sir Posseth. It's not worth the discussion.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top