使用数据注释显示时间和数据 [英] Show Time and data using Data annotation

查看:133
本文介绍了使用数据注释显示时间和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是最适合时间值的数据类型,并且是否也有相同的数据注释?

I wanted to know whats is the best data type for time value only also is there a data annotation for the same ?

我还想知道是否有一种方法可以使用数据注释来格式化数据类型datetime以显示日期和时间(例如:dd-mm-yy HH:MM AM/PM)

Also I wanted to know is there a way to format datatype datetime using Data annotation to display date and time(eg: dd-mm-yy HH:MM AM/PM)

推荐答案

如果对象的类型为DateTime,则只需将格式字符串作为参数传递给,就可以使用.ToString()方法以所需格式打印它. ToString().考虑以下示例:

If the object is of type DateTime then you can simply use .ToString() method for print it in required format, by passing the format string as argument to the .ToString(). Consider the following example:

 DateTime currentDate = DateTime.Now; // let it be 27-06-2016 13:06
 string format = "dd-MM-yy HH:MM tt";
 string formatedDateTime = currentDate.ToString(format, CultureInfo.InvariantCulture);

注意::您可以找到更多格式选项这里,以及一个工作示例这里

Note : You can find more format options here,and a working example here

这篇关于使用数据注释显示时间和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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