将前导零添加到日期时间值 [英] Add leading zero to datetime value

查看:47
本文介绍了将前导零添加到日期时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,但很难为我做.我有一个日期时间值.

I have a easy question but hard to do for me. I have a date time value.

DateTime? Date;

日期的值类似于 4/2/2014 .我如何将此值更改为 04/02/2014 ?

Date has value like 4/2/2014. How can i change this value to 04/02/2014?

推荐答案

DateTime 没有具有格式-仅在时才获得格式显示.如果您未指定格式(例如在调试器中查看格式),则会根据控件类型和当前区域性选择默认格式.

A DateTime doesn't have a format - it only gets a format when it's displayed. If you don't specify a format (like when viewing in the debugger), a default one will be chosen depending on the control type and current culture.

正确的答案取决于您显示日期的方式.如果您使用的是 ToString :

So the right answer depends on how you're displaying the date. If you're using ToString:

Date.Value.ToString("MM/dd/yyyy")

如果您使用某种控件,请阅读有关如何指定值格式的文档.

If you're using some sort of control read the docs on how to specify value formats.

这篇关于将前导零添加到日期时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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