473,595 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DateTime: Huh?



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

myDate = DateTime.Now;

and

myDate = DateTime.Parse( DateTime.Now.To String("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.ExecuteNonQ uery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedur e'
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 3728
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.co m
"ChrisM" <hi****@AskMeIf YouWantIt.com> wrote in message
news:eR******** ******@TK2MSFTN GP12.phx.gbl...


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

myDate = DateTime.Now;

and

myDate = DateTime.Parse( DateTime.Now.To String("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.ExecuteNonQ uery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedur e'
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****@AskMeIf YouWantIt.com> wrote in message
news:eR******** ******@TK2MSFTN GP12.phx.gbl...


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

myDate = DateTime.Now;

and

myDate = DateTime.Parse( DateTime.Now.To String("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****@AskMeIf YouWantIt.com> wrote:
Could anyone please tell me the difference between these 2 lines:

myDate = DateTime.Now;

and

myDate = DateTime.Parse( DateTime.Now.To String("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.ExecuteNonQ uery();

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

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.co m>
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****@AskMeIf YouWantIt.com> wrote in message
news:eR******** ******@TK2MSFTN GP12.phx.gbl...


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

myDate = DateTime.Now;

and

myDate = DateTime.Parse( DateTime.Now.To String("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.ExecuteNonQ uery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedur e'
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.To String("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.ExecuteNonQ uery();

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


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.To String("dd/MM/yyyy"));

Console.WriteLi ne( d1);
Console.WriteLi ne( 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.Da te

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****@AskMeIf YouWantIt.com> wrote in message
news:eR******** ******@TK2MSFTN GP12.phx.gbl...


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

myDate = DateTime.Now;

and

myDate = DateTime.Parse( DateTime.Now.To String("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.ExecuteNonQ uery();

The command object is a Access update query with a single date parameter.
Connection string is Jet. Command type is 'StoredProcedur e'
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.To String("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.ExecuteNonQ uery();

The command object is a Access update query with a single date
parameter. Connection string is Jet. Command type is
'StoredProcedur e'
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.CurrentT hread.CurrentCu lture = new CultureInfo("nl-NL");
myDate = DateTime.Parse( DateTime.Now.To String("dd/MM/yyyy"));


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

:-))

Cor
Nov 15 '05 #9
Tim Jarvis <tj*****@NoSpam ForMe.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

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

Similar topics

4
2518
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 should be used? I can make a datetime easily enough
16
10465
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 using C# eg. - String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from the database. - I do not care about the time portion, I only want "1/1/2005" for display.
15
14262
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" DateTime.Now.AddMinutes(90) returns "1:30" or "01:30"
3
4231
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 datatable feeding the datagrid control. Both are of the DateTime Type. How do I get the time field to display only the Time, and not the date, which is apparently the default.
6
8973
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 between date and datetime classes? Please, help
5
1992
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second); I keep getting the below error: Object reference not set to an instance of an object. I don't know what the problem could be. Can someone help me with this? It
26
2663
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' How do i do that? Reny ---
11
7209
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 only that I have to listen to them because they know it better. They told also that in a business situation it is better to use datetime.parseexact for changing cultures and not to use the globalization setting. I did not give them this sample,...
9
4908
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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:JadeWebServices/WebServiceProvider/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
0
16485
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 formatting considerations at all. They are simple, easy, and brief and you should use them any time you need to incorporate any date literals or date math in your T-SQL code. create function DateOnly(@DateTime DateTime) -- Returns @DateTime...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8019
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8251
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5839
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.