DateTime C#,如何使用 [英] DateTime c#, how to use

查看:122
本文介绍了DateTime C#,如何使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


使用DateTime.
我可以声明日期时间吗?像:

Hi,
using DateTime.
Can i declare DateTime? like:

int intnumber ;


我们可以写:


can we write:

DateTime dtdate ;


并珍惜它,例如:


and value it, like:

dtdate = 6/18/2012 09:54:22 ;


我想从输入中读取类似"6/18/2012 09:54:22"的文本.
然后将其保存到文件中.
然后从该文件中读取它,并使其不作为字符串,将其作为DateTime.
以及DateTime方法是什么样的:


i want to read a text like "6/18/2012 09:54:22" from input.
then save it into a file.
then read it from that file, and have it not as an string have it as a DateTime.
and what are DateTime method''s like:

DateTime.Now

?

推荐答案

您可以创建一个新的DateTime值通过以下任何一种方式:

通过调用DateTime 构造函数的任何重载,这些重载允许您指定日期和时间值的特定元素(例如年,月,日或节拍数).
以下语句说明了调用DateTime 构造函数之一以创建具有特定年,月,日,时,分和秒的日期.
You can create a new DateTime value in any of the following ways:

By calling any of the overloads of the DateTime constructor that allow you to specify specific elements of the date and time value (such as the year, month, and day, or the number of ticks).
The following statement illustrates a call to one of the DateTime constructors to create a date with a specific year, month, day, hour, minute, and second.
DateTime date1 = new DateTime(2008, 5, 1, 8, 30, 52);



通过为DateTime 对象分配由属性或方法返回的日期和时间值.
下面的示例将当前日期和时间,当前世界标准时间(UTC)日期和时间以及当前日期分配给三个新的DateTime 变量. />



By assigning the DateTime object a date and time value returned by a property or method.
The following example assigns the current date and time, the current Coordinated Universal Time (UTC) date and time, and the current date to three new DateTime variables.

DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;
DateTime date3 = DateTime.Today;



通过解析日期和时间值的字符串表示形式.
Parse, ParseExact, TryParse, and TryParseExact方法都将字符串转换为其等效的日期和时间值.
下面的示例使用Parse方法解析字符串并将其转换为DateTime值.



By parsing the string representation of a date and time value.
The Parse, ParseExact, TryParse, and TryParseExact methods all convert a string to its equivalent date and time value.
The following example uses the Parse method to parse a string and convert it to a DateTime value.

string dateString = "19/6/2012 10:51:52 AM";
DateTime date1 = DateTime.Parse(dateString, 
                          System.Globalization.CultureInfo.InvariantCulture);



通过调用DateTime 结构的隐式默认构造函数. (有关值类型的隐式默认构造函数的详细信息,请参见值类型"(C#参考).)对于支持该值的编译器,一个近似等效项声明一个DateTime值,而没有为其显式分配日期和时间. > 下面的示例说明了在C#和Visual Basic中对DateTime 隐式默认构造函数的调用,以及在Visual Basic中没有赋值的DateTime 变量声明.



By calling the DateTime structure''s implicit default constructor. (For details on the implicit default constructor of a value type, see Value Types (C# Reference).) An approximate equivalent, for compilers that support it, is declaring a DateTime value without explicitly assigning a date and time to it.
The following example illustrates a call to the DateTime implicit default constructor in C# and Visual Basic, as well as a DateTime variable declaration with no assignment in Visual Basic.

DateTime dat1 = new DateTime();
// The following method call displays 1/1/0001 12:00:00 AM.
Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture));
// The following method call displays True.
Console.WriteLine(dat1.Equals(DateTime.MinValue));


参考号:日期时间结构 [ C#DateTime [ ^ ]
C#DateTime格式 [ DateTime [C#]的字符串格式 [


Ref.:DateTime Structure[^]

Also have look on some more useful links:
C# DateTime[^]
C# DateTime Format[^]

Some more:
String Format for DateTime [C#][^]


看看下面的链接,您可能会得到您需要什么:

http://www.dotnetperls.com/datetime-format [ http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ]

轻松将字符串转换为DateTime,将DateTime转换为字符串和格式 [ ^ ]
Have a look at below links, you might get what you need:

http://www.dotnetperls.com/datetime-format[^]

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^]

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^]

Easy String to DateTime, DateTime to String and Formatting[^]


这是有关日期时间的教程:
http://www.dotnetperls.com/datetime [ http://www.csharp-examples.net/string-format-datetime/ [ ^ ]

转换示例:
http://msdn.microsoft.com/en-us/library/cc165448.aspx [ ^ ]
Here is a tutorial on datetime:
http://www.dotnetperls.com/datetime[^]

Formatting examples:
http://www.csharp-examples.net/string-format-datetime/[^]

conversion examples:
http://msdn.microsoft.com/en-us/library/cc165448.aspx[^]


这篇关于DateTime C#,如何使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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