Solved

c# .net DateTime without Time

Posted on 2011-03-02
6
457 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - SQL Timeout 9 49
Sort GridView by ID Descending 1 24
VS 2013 installer,  Install, Update and uninstall via SCCM 6 43
c# DateTime Format validation 4 67
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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