473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

DateTime: Huh?



Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

....

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedure'
Please no-one make any comments about the overload of 'Parameter.Add' I'm
using, I know that if I constructed my parameters properly, I wouldn't get
the problem. I'm just interested as to why one version works and the other
doesn't.

Thanks,

ChrisM.
Nov 15 '05 #1
14 3700
ChrisM,

While you asked for no comments about the overload of the Add method, if
you know it will work if you construct the parameters properly, then why not
use that?

That being said, the difference between the first and second line, in
terms of results, should be in the time portion of the value only. The
parsing of the datetime does not yield a value for time in the second line,
while the first line will have the time of day included in it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...


Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedure'
Please no-one make any comments about the overload of 'Parameter.Add' I'm
using, I know that if I constructed my parameters properly, I wouldn't get
the problem. I'm just interested as to why one version works and the other
doesn't.

Thanks,

ChrisM.

Nov 15 '05 #2
Hi Chris,

"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...


Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));


Besides Nicholas' observations, I will point out that the above will
swap the date and month (assuming your current culture is en-us).
DateTime.Parse will assume a date format of "MM/dd/yyyy", but you are giving
it "dd/MM/yyyy".

Regards,
Dan
Nov 15 '05 #3
ChrisM <hi****@AskMeIfYouWantIt.com> wrote:
Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedure'

Please no-one make any comments about the overload of 'Parameter.Add' I'm
using, I know that if I constructed my parameters properly, I wouldn't get
the problem. I'm just interested as to why one version works and the other
doesn't.


I suspect it's guessing that the parameter is a date rather than a
datetime, as you'll only have non-zero date information. Just create
the parameter fully though, I'd recommend.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Thanks for your comments Guys.

Nicholas: I will construct the parameters properly, I was just interested in
why it wasn't working as I expected.

Cheers,
ChrisM

"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...


Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedure'
Please no-one make any comments about the overload of 'Parameter.Add' I'm
using, I know that if I constructed my parameters properly, I wouldn't get
the problem. I'm just interested as to why one version works and the other
doesn't.

Thanks,

ChrisM.

Nov 15 '05 #5
ChrisM wrote:
Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedure'


Is it because the date is not what the query expects in the second case?

here's what I get:

-- code -----------
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));

Console.WriteLine( d1);
Console.WriteLine( d2);

-- output ----------
3/5/2004 10:37:37 AM
5/3/2004 12:00:00 AM

--
mikeb
Nov 15 '05 #6
ChrisM,
As the others have pointed out, the first includes Date & Time, while the
second includes only a Date.

I would recommend the following instead of the
DateTime.Parse(DateTime.Now...):

myDate = DateTime.Today

or

myDate = DateTime.Now.Date

As you are not susceptible to culture settings where dd/MM/yyyy may yield an
invalid date in foreign countries.

Hope this helps
Jay

"ChrisM" <hi****@AskMeIfYouWantIt.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...


Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the first
option, but not with the second. (I get : {"Data type mismatch in criteria
expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedure'
Please no-one make any comments about the overload of 'Parameter.Add' I'm
using, I know that if I constructed my parameters properly, I wouldn't get
the problem. I'm just interested as to why one version works and the other
doesn't.

Thanks,

ChrisM.

Nov 15 '05 #7
ChrisM wrote:


Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
Seeing as later on in the code, the ExecuteNonQuery works with the
first option, but not with the second. (I get : {"Data type mismatch
in criteria expression." })

Private OleDbCommand cmd;

...

cmd.Parameters.Add("issueDate",myDate
cmd.ExecuteNonQuery();

The command object is a Access update query with a single date
parameter. Connection string is Jet. Command type is
'StoredProcedure'
Please no-one make any comments about the overload of 'Parameter.Add'
I'm using, I know that if I constructed my parameters properly, I
wouldn't get the problem. I'm just interested as to why one version
works and the other doesn't.

Thanks,

ChrisM.


IIRC the sql92 standard for date time formats requires that the date is
represented as yyyy/mm/dd (it is the same as ISO 8601), but it is very
common for mm/dd/yyyy to be accepted as well. however the format
dd/mm/yyyy is almost never accepted, even if your international
settings are set to this.

Your second format specifies the exact date format to use, whereas your
first format will return whatever your short date form is in your
settings i.e. in America it will be MM/dd/yyyy. (on my system here in
the UK both return the same)

Cheers Tim.
Nov 15 '05 #8
Cor
I do also an addition.

If you want to use this, to make it more sure for the globalization you
could add by instance.

Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));


I am curious what Miha and both Willams add to this thread,

:-))

Cor
Nov 15 '05 #9
Tim Jarvis <tj*****@NoSpamForMe.com> wrote:
IIRC the sql92 standard for date time formats requires that the date is
represented as yyyy/mm/dd (it is the same as ISO 8601), but it is very
common for mm/dd/yyyy to be accepted as well. however the format
dd/mm/yyyy is almost never accepted, even if your international
settings are set to this.

Your second format specifies the exact date format to use, whereas your
first format will return whatever your short date form is in your
settings i.e. in America it will be MM/dd/yyyy. (on my system here in
the UK both return the same)


No, it doesn't specify the format for sending the DateTime up to the
database in at all - it just specifies the DateTime value itself.
There's no format information implicit in a DateTime.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
Jon Skeet [C# MVP] wrote:
No, it doesn't specify the format for sending the DateTime up to the
database in at all - it just specifies the DateTime value itself.
There's no format information implicit in a DateTime.


Hi Jon,

Well, that's actually what I thought as well, but try this (or your
local variant)

DateTime MyDate1 = DateTime.Now;
DateTime MyDate2 = DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy"));
MessageBox.Show(MyDate1 + Environment.NewLine + MyDate2);

this gives me (here in the UK)

07/03/2004 11:39:23
03/07/2004 00:00:00

What I had expected was to see was the same value for both variables,
as they are both DateTimes and both calling the ToString() method, the
metod of creation was different that's all. So it would seem that it
does make a difference how you construct your DateTime variable.

Cheers Tim.
Nov 15 '05 #11
Tim Jarvis <tj*****@NoSpamForMe.com> wrote:
Jon Skeet [C# MVP] wrote:
No, it doesn't specify the format for sending the DateTime up to the
database in at all - it just specifies the DateTime value itself.
There's no format information implicit in a DateTime.
Hi Jon,

Well, that's actually what I thought as well, but try this (or your
local variant)

DateTime MyDate1 = DateTime.Now;
DateTime MyDate2 = DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy"));
MessageBox.Show(MyDate1 + Environment.NewLine + MyDate2);

this gives me (here in the UK)

07/03/2004 11:39:23
03/07/2004 00:00:00


Yes, and so it would - because the second way you've constructed it has
only parsed the date part, and not the time part.
What I had expected was to see was the same value for both variables,
as they are both DateTimes and both calling the ToString() method, the
metod of creation was different that's all. So it would seem that it
does make a difference how you construct your DateTime variable.


No, it doesn't make any *formatting* difference - it's just that the
way you've constructed the second DateTime doesn't provide it any time
information.

If you have two DateTimes with different amounts of information then
yes, they will display differently. That's not the same as the DateTime
itself having any format information associated with it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12
"Tim Jarvis" wrote:

DateTime MyDate1 = DateTime.Now;
DateTime MyDate2 = DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy"));
MessageBox.Show(MyDate1 + Environment.NewLine + MyDate2);

this gives me (here in the UK)

07/03/2004 11:39:23
03/07/2004 00:00:00

What I had expected was to see was the same value for
both variables, as they are both DateTimes and both
calling the ToString() method, the metod of creation
was different that's all. So it would seem that it
does make a difference how you construct your
DateTime variable.


If we split your example a bit, it might show what's really happening:

// First the date is created

DateTime MyDate1 = DateTime.Now;

// You construct a string containing the date
// in the order of month/day/year-parts

string date1String = DateTime.Now.ToString("MM/dd/yyyy");

// date1String now contains "03/07/2004"

// But when you parse it, the Parse-method doesn't know
// in what order the parts are, and believes based on
// your locale that it's in the order day/month/year

DateTime MyDate2 = DateTime.Parse(date1String);

If you want to parse a string into a DateTime, where the string doesn't
follow your locale, you can use one of the overloaded methods for Parse,
where you can use an IFormatProvider.

// Bjorn A

Nov 15 '05 #13
Jon Skeet [C# MVP] wrote:

Yes, and so it would - because the second way you've constructed it
has only parsed the date part, and not the time part.


Actually I wasn't thinking about the time portion I was thinking about
the dd and MM portions, but rather stupidly forgot that 03/07/2004 and
07/03/2004 are both valid dates, and in my UK settings are
(respectively) 3rd July and 7th March. My second line was simply
creating a different date from portions of the first date, and was just
lucky not to barf.

I think that the original poster is doing the same thing, i.e.
constructing a DateTime variable using a format that is different from
the internationalization format.

Cheers Tim.

Nov 15 '05 #14
Tim Jarvis <tj*****@NoSpamForMe.com> wrote:
I think that the original poster is doing the same thing, i.e.
constructing a DateTime variable using a format that is different from
the internationalization format.


No, because (as I said before) the DateTime itself doesn't have any
format associated with it at all. It's just a date and time - except
that in his case, the second version doesn't really have any time
information.

Note that the problem doesn't come at the parsing stage (as far as the
OP indicated, anyway) - it comes when the DateTime is being used as a
parameter value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
3
by: Andrew S. Giles | last post by:
Hello, I am importing a flat text file, and putting it into a datagrid for display on a form. Currently the users have their dates and times seperated. I have two fields, therefore in the...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
5
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new...
26
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM'...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.