473,486 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert DateTime.Now() to SQL Server(tm) standard internal format fordatetime

Hi,

How can I convert a value created from DateTime.Now() to the datetime
format that SQL Server recognises?
Thanks in advance.

Feb 6 '08 #1
3 30778
Hi,

Just like that :)

//If you are using parameters THE PREFERRED WAY
com.Parameters.Add("@adate", SqlDbType.DateTime).Value = DateTime.Now;

//building the text
com.CommandText = String.Format("update T set aDate = '{0}' ",
DateTime.Now.ToShortDateString() );

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
<hu********@gmail.comwrote in message
news:f9**********************************@s19g2000 prg.googlegroups.com...
Hi,

How can I convert a value created from DateTime.Now() to the datetime
format that SQL Server recognises?
Thanks in advance.

Feb 6 '08 #2
//building the text
com.CommandText = String.Format("update T set aDate = '{0}' ",
DateTime.Now.ToShortDateString() );
I know it was only presented as the second option, but just to note
that even in this case you are probably at risk if the client has a
different locale to the server; which is yet another reason to use
parameters (aimed at the OP).

Format aside, you may also (depending on the system) have to worry
about timezones ;-p
UTC is never a bad choice in such scenarios...

Marc
Feb 6 '08 #3
It's not guaranteed that the current thread culture is the one that the
server will use for processing.

Rather, if you MUST use a literal in a constructed string, you would use
the format:

yyyy-MM-dd HH:mm:ss.fff

This will represent down to milliseconds (which is the highest
resolution of the datetime data type on SQL Server). You can choose to omit
anything from one point in the string on (for example, everything from hour
(HH) on), depending on the comparison being made.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:uY**************@TK2MSFTNGP05.phx.gbl...
Hi,

Just like that :)

//If you are using parameters THE PREFERRED WAY
com.Parameters.Add("@adate", SqlDbType.DateTime).Value = DateTime.Now;

//building the text
com.CommandText = String.Format("update T set aDate = '{0}' ",
DateTime.Now.ToShortDateString() );

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
<hu********@gmail.comwrote in message
news:f9**********************************@s19g2000 prg.googlegroups.com...
>Hi,

How can I convert a value created from DateTime.Now() to the datetime
format that SQL Server recognises?
Thanks in advance.


Feb 6 '08 #4

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

Similar topics

3
2776
by: andrewcw | last post by:
I save date time into my XML files like this: MesObj.theTime=System.DateTime.Now; "5/4/2004 8:50:30 AM" BUT that string is NOT recognizable as a valid dateTime when the data is read back from...
2
15537
by: mp | last post by:
I am using : SystemDate= System.DateTime.Now.Year.ToString() +System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString(); ...
2
39387
by: Q. John Chen | last post by:
I'd like to convert a DateTime value into XML date format. 1999-02-03T00:00.0000000-05:00 What is the easy way?
1
3081
by: ABC | last post by:
How to convert a date string to datetime value with custom date format? e.g. Date String Date Format Result (DateTime value) "05/07/2004" "MM/dd/yyyy" May 7, 2004 "01062005" ...
2
4769
by: ziggislaw | last post by:
hello how can I convert DateTime from "25.12.2005" to "2005-12-25 00:00:00.000" ? Now I have DateTime as string (I get it from <asp:label>). Thanks
0
1142
by: shapper | last post by:
Hello, I am converting a DateTime variable to a string in RFC1123 format (RSS) using the following code: Dim a As DateTime a = DateTime.Now Dim b As String = a.ToString("R") It works.
3
5332
by: huohaodian | last post by:
Hi, How can I convert a value created from DateTime.Now() to the datetime format that SQL Server recognises? Thanks in advance.
3
15964
by: Curious | last post by:
I have code below. But it won't compile. Error: Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime' DateTime now = DateTime.Now.TimeOfDay; DateTime openTime =...
0
2107
by: Curious | last post by:
I have the code below. But it won't compile. //Compiling error: Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime' DateTime now = DateTime.Now.TimeOfDay; DateTime...
3
17678
by: raulbolanos | last post by:
Hello guys, I get this time from DateTime.Now "6/12/2009 11:45:28 AM" and I need to convert it ISO 8601 which is the one that SQL accepts "2009-12-06 11:45:28". What can I do?
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
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...
0
7132
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,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
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...
0
3076
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...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.