Solved

c# .net DateTime without Time

Posted on 2011-03-02
6
456 Views
Last Modified: 2012-05-11
Is there a way of setting a DateTime, and persisting that to SQL Server DateTime field and distinguish between those with times and those without, without using a 'special' time like midnight?
E.g. I want a datetime field to hold NextAppointmentDate, which may or may not have a Time, but I don't want to store a seperate Time field, is that possible?
0
Comment
Question by:Silas2
6 Comments
 
LVL 9

Expert Comment

by:s_chilkury
ID: 35016877
I think this question is in regards of SQL DateTime field.

If you wish to store a date (NextAppointmentDate) then you need to store that in Date field in case of SQL Server 2008. This is not the case with SQL Server 2005 or less. In that case you need to store date field excluding time specifying the time as 00:00:00.
0
 

Author Comment

by:Silas2
ID: 35016939
You can't say 'the time element is null' somehow? Its just that someone might want to use midnight.
0
 
LVL 9

Expert Comment

by:s_chilkury
ID: 35017065
Yes, for SQL Server 2005 or below versions we dont have time element as NULL. But with SQL Server 2008 we have a new datatype which is date and time as separate datatypes.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Jini Jose
ID: 35017125
you can use the datetime field.
and while passing the datetime value from c# you can format it with yyyy-Mm-dd hh:mm:ss.

if it has time then the datetime value will be like this 2011-Mar-03 10:20:33
and if it not has time then the datetime value will be like this 2011-Mar-03 00:00:00

0
 

Author Comment

by:Silas2
ID: 35017898
Yes gmailjini: but that is midnight, isn't it? Say someone wanted to use midnight? wouldn't that get confused with 'time=null'?
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 125 total points
ID: 35022259
The answer is that you need two separate fields in your database to accomplish this, one for Date and one for Time. .Net has no notion of a date-only type.

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
use of unassigned local variable 'id' 6 52
Need to start a web service from Visual Studio 2015 Pro 2 35
defining NULL or 0 10 44
Why use this lambda? 12 26
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question