Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having an class with some properties of different data types, and for one of them it should be of Date type.
But unfortunately I am not able to find any data type with Date but instead we are having only DateTime as data type available, which contains the default time-stamp as 00:00:00, which I wanted to eliminate and my object should hold only date.

Please help in identifying the appropriate solution.
Posted
Comments
Richard MacCutchan 6-Jan-15 5:12am    
Use the DateTime, it contains all the functionality you need, and the zero time does not cost anything.
KUMPREK 6-Jan-15 5:16am    
Richard: Yes I understand that DateTime is having all the functionality which I am looking for but there is requirement to discard the time stamp.
The only solution I can think of is create another property and in the getter section of the property I will only return the Date part from DateTime.

But its very strange that C# don't provide data type for Date.
Richard MacCutchan 6-Jan-15 12:39pm    
Because you don't need one. If you only want the date information then just set the time value to zero and you will have everything you need. What is your difficulty with using it?
[no name] 7-Jan-15 7:18am    
u can remove the time stamp part using sql.
Neetin_1809 7-Jan-15 9:00am    
Why Not You Try DateTime.Now.ToString("dd-MMM-yyyy") ??

You can not find a Date class because there is none...The only thing you can do is writing a Date class (or find one using Google) for yourself...
 
Share this answer
 
Comments
KUMPREK 6-Jan-15 5:17am    
Peter: The other solution that I could also think of is create another property and in the getter section of the property I will only return the Date part from DateTime.

But its very strange that C# don't provide data type for Date.
Kornfeld Eliyahu Peter 6-Jan-15 5:19am    
It's a known lack of the .NET framework...Not much to do...
Kornfeld Eliyahu Peter 6-Jan-15 5:20am    
By using date part of DateTime class you play with time-zones too...If you create your own you will not have time-zone issues...
CHill60 7-Jan-15 6:50am    
My 5 - the OP posted a summary of what you said as their own solution and then accepted it as the answer!
Edit - and to be fair OP has now deleted that solution
Kornfeld Eliyahu Peter 7-Jan-15 6:56am    
Thank you...
There is no need for a specialized Date class because the DateTime object has a 'Date Property that automatically strips off the time information for you, and, it's so easy to wrap a DateTime struct in a Class and make it do whatever you want to do.

Create a DateTime in code, then inspect the 'Date Property: notice how it sets the time components of the DateTime Struct to certain default values: you'll see that Hour, Minute, Second, Millisecond are zeroes, and the 'TimeOfDay property is (00:00:00). Inspect the 'TimeOfDay property (a read-only struct): notice how all the fields whose names start with 'Total are set to zero.

You can take in a DateTime, use the current Culture settings to map it to UTC, get the UTC Date only, null out the Hour, Minutes, Milliseconds, etc.; easy:
C#
public class UTCDate
{
    public DateTime TheDate { private set; get; }

    // 'ctor
    public UTCDate(DateTime initialDate)
    {
        this.TheDate = initialDate.ToUniversalTime().Date;
    }

    // alternate string 'ctor
    public UTCDate(string dateCandidate)
    {
        DateTime testDate;

        if(DateTime.TryParse(dateCandidate, out testDate))
        {
            TheDate = testDate.ToUniversalTime().Date;
        }
        else
        {
            // throw error
            throw new ArgumentException("Invalid string input to UTCDate");
        }
    }
}

// test in a method or EventHandler: set break-points; examine values

  UTCDate utcDate1 = new UTCDate(DateTime.Now);

  UTCDate utcDate2 = new UTCDate(utcDate1.TheDate.AddHours(-16));

  UTCDate utcDate3 = new UTCDate("12/08/1999 13:12");
 
Share this answer
 
v2
As Kornfeld Eliyahu Peter[^] mentioned there is no way to use Date object (as VB), because it doesn't exists.

If you want to ignore time part to be able to display only date, just format it:
C#
Console.WriteLine(DateTimeVariable.ToString("yyyy/MM/dd"));


Simple?

See: DateTime[^]
 
Share this answer
 
Comments
KUMPREK 7-Jan-15 6:31am    
Maciej Los: My question was not how to show the date stuff, that I can of course do it as you mentioned, my question was how to have a property only to store date and exclude time. And seams that .Net don't have the possibility to do so..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900