C#中的日期对象而不是DateTime [英] Date Object in C# instead of DateTime

查看:77
本文介绍了C#中的日期对象而不是DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类有一些不同数据类型的属性,其中一个属性应该是 Date 类型。

但遗憾的是,我无法找到日期的任何数据类型,而是我们只有 DateTime 作为可用的数据类型,其中包含默认时间戳为 00:00:00 ,我想消除它,我的对象应该只保留日期。



请帮助确定合适的解决方案。

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.

推荐答案

你找不到Date类,因为那里是没有...你唯一能做的就是为自己编写一个Date类(或者使用Google找一个)...
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...


不需要专门的Date类,因为DateTime对象有一个'Date属性,可以自动为你删除时间信息,并且可以很容易地将一个DateTime结构包装在一个Class中并让它做你想做的任何事情。



在代码中创建一个DateTime,然后检查'Date属性:注意它如何将DateTime Struct的时间组件设置为某些默认值:你会看到Hour,Minute,Second,Millisecond是零, 'TimeOfDay属性是(00:00:00)。检查'TimeOfDay属性(只读结构):注意名称以'Total'开头的所有字段如何设置为零。



你可以接受DateTime,使用当前的Culture设置将其映射到UTC,仅获取UTC Date,null,Hour,Minutes,Milliseconds等; easy:
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:
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");


As Kornfeld Eliyahu Peter [ ^ ]提到没有办法使用Date对象(如VB),因为它不存在。



如果你想忽略时间部分只能显示日期,只需格式化它:

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:
Console.WriteLine(DateTimeVariable.ToString("yyyy/MM/dd"));





简单?



参见:日期时间 [ ^ ]


这篇关于C#中的日期对象而不是DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆