如何获取系统的日期和时间 [英] how to get the date and the time of the system

查看:123
本文介绍了如何获取系统的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:)

我想要这样的日期和时间格式:2011-09-28 05:44:23 AM

我使用日期

hi all :)

I want to get the date and time format like this: 2011-09-28 05:44:23 AM

I useDate

Time _DateTime = DateTime.Now;
string _justdate = _DateTime.Date.ToString();



但这给了我与time = 12:00:00 AM
约会的日期
如何获取日期和时间以将其插入到数据库表的datetime字段中

请帮忙吗? :)



but it gives me alawys that date with time = 12:00:00 AM

how can I get the date and the time to insert it to datetime field at my database table

any help please ? :)

推荐答案

因为您转换了_DateTime并使用了变量的Date部分的ToString.您可以直接使用DateTime.Now.ToString()来获取计算机的日期时间.
Because you convert the _DateTime and use the ToString of the Date part of the variable. You can directly use DateTime.Now.ToString() to get datetime of the machine.


不要单独使用日期和时间.从某种角度看,没有日期"之类的东西.这实际上只是时间点的子集(范围,程序员说,不太正确).在您的情况下,属性Date是无用的.

您真正需要的是使用适当的格式说明符对时间进行格式化.查看所有方法System.DateTime.ToString和格式说明符:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].

请注意,System.Globalization.CultureInfo支持IFormatProvider作为System.DateTime.ToString(IFormatProvider)的参数,请参见 http://msdn.microsoft.com/en-us/library/ht77y576.aspx [ ^ ].

您可以使用

Don''t work with date and time separately. From a certain point of view, there is no such thing as "date"; this is really just a sub-set of time points (range, programmers say, not quite correctly). In your case, the property Date is useless.

What you really need is time formatted using appropriate format specifier. See all the methods System.DateTime.ToString and format specifiers:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Note that IFormatProvider is supported by System.Globalization.CultureInfo for the parameter of System.DateTime.ToString(IFormatProvider), see http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^].

In your case, you can use

System.DateTime myTime = System.DateTime.Now;
string myTimeString = myTime.ToString("yyyy-MM-dd HH:mm:ss tt");



您没有说明中立或本地化申请AM/PM所需的文化.

请参阅上面有关如何本地化AM/PM的参考,请参见代码示例: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#ttSpecifier [ ^ ].

—SA



You did not explain what culture you need to apply for AM/PM, neutral or localized.

See the above references on how to have AM/PM localized, see the code sample: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#ttSpecifier[^].

—SA


这篇关于如何获取系统的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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