简体   繁体   English

C#中的日期时间

[英]DateTime In C#

In C# if I want to parse a datetime, but some times I just have either a date and not a time component or no date but a time component, how would I do this? 在C#中如果我想解析一个日期时间,但有时候我只有一个日期而不是时间组件或没有日期而是一个时间组件,我该怎么做? Usualy when you leave out the time component, it automaticly assumes that the time is 12:00AM. 通常,当您省略时间组件时,它会自动假定时间是12:00 AM。 But I don't want this. 但我不想要这个。 If the time component is missing then I just want the DateTime to store a date only and the leave the time component off. 如果缺少时间组件,那么我只希望DateTime仅存储日期并保留时间组件。

内部DateTime的值只是一个UInt64 (C#中的ulong ),它存储了自上一个日期以来的刻度数,因此无论您是否喜欢,时间组件将始终存在。

If you only need to display certain parts, just use any of the format strings (examples are for "en-us" culture): 如果您只需要显示某些部分,只需使用任何格式字符串(例如“en-us”文化):

DateTime.Now.ToString("d"); // 5/26/2009
DateTime.Now.ToString("t"); // 4:56 PM

The complete reference: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx 完整参考: http//msdn.microsoft.com/en-us/library/az4se3k1.aspx

It's not possible to have a DateTime without a time component. 没有时间组件的DateTime时间是不可能的。 You could store a boolean flag along with it in a struct to store data about existence of that component. 您可以将一个布尔标志与它一起存储在struct以存储有关该组件存在的数据。 However, there's no way to use the automatic parsing routine to distinguish between a DateTime string with a time specified as 12:00 PM and a nonexistent one. 但是,没有办法使用自动解析例程来区分DateTime字符串与指定为12:00 PM和不存在的时间。

如果它真的让你烦恼,你总是可以创建一个包装类来隐藏datetime类的时间部分。

No you will have the time component no matter what. 不管你怎么做都不会有时间成分。 The best you can do is access the Date property on your DateTime object if you really have to. 您可以做的最好是访问DateTime对象上的Date属性,如果您真的必须这样做。

http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx

DateTime by definition stores a date and a time such that it cannot just represent one of them without representing the other. 根据定义,DateTime存储日期和时间,使得它不能仅代表其中一个而不代表另一个。 If you only want the date (or only the time), parse out the information you need and discard the rest of it. 如果您只想要日期(或只是时间),请解析您需要的信息并丢弃其余部分。

As mentioned before DateTime will always have a Date and a Time part of it if you only want a single part use the way described by the others 如前所述,如果您只希望单个部件使用其他部件描述的方式,则DateTime将始终具有日期和时间部分

DateTime date = DateTime.Parse("2009-11-30);
date.Year; = 2009
date.Month; = 11
date.Day; = 30
date.Hour; = 0
and so on

The thing you must be aware is that all of these methods will only return an integer. 你必须意识到的是,所有这些方法都只返回一个整数。

If you want to know all the possible ways to parse a string John Sheehan has put together a great Cheat Sheet wit all possible ways to parse and manipulate dates , and other strings for that matter. 如果你想知道解析字符串的所有可能方法,John Sheehan已经整理了一个很好的Cheat Sheet ,以及解析和操作日期的所有可能方法 ,以及其他字符串。

您可以拥有一个存储DateTime的类,并确定是否设置了时间,或者是否只设置了日期并相应地返回值。

A DateTime object is always stores a date + a time, not just one. DateTime对象始终存储日期+时间,而不仅仅是一个。 You can always choose to work only with the date part, ie only use properties like Year, Month, DayOfWeek. 您始终可以选择仅使用日期部分,即仅使用Year,Month,DayOfWeek等属性。 But underneath there will aways be some stored time. 但在下面会有一些存储时间。

Use 使用

DateTime date = new DateTime();
date = DateTime.Parse("1/1/2001"); 

to set the date, then use 设置日期,然后使用

date.ToShortDateString();

or 要么

date.Year;
date.Month;
date.Day;

to get what you need. 得到你需要的东西。 Hope that helps! 希望有所帮助!

It is very dangerous to assume that the date portion of a DateTime is necessarily the date you are expecting. 假设DateTime的日期部分必然是您期望的日期,这是非常危险的。 As pointed-out, DateTime always includes and considers the time aspect, even when you don't see it. 正如所指出的, DateTime总是包含并考虑时间方面,即使您没有看到它。

This is a big problem when you have data stored in different time-zones (and particularly if knowledge of that offset is not also kept, because it is assumed that what is being stored is a Date, not a date-with-time). 当您将数据存储在不同的时区时(特别是如果不保留该偏移的知识,这是一个大问题,因为假设存储的是日期,而不是日期与时间)。

You may store a birthdate as '01/01/2000 00:00:00' during Summer-Time, which then is stored in UCT as '31/12/1999 23:00:00'. 您可以在夏令时将生日日期存储为'01 / 01/2000 00:00:00',然后将其作为'31 / 12/1999 23:00:00'存储在UCT中。 When you then read that birth-date later, the date portion is now a day early. 当您稍后阅读出生日期时,日期部分现在提前一天。

Best to create your own type. 最好创建自己的类型。 Strange that Microsoft didn't think it worth having a Date type. 奇怪的是,微软并不认为值得拥有Date类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM