DateTime转换在c# [英] DateTime convert in c#

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

问题描述

我有 DateTime 此格式的变量dd / mm / yyyy hh:mm:ss
我想将其转换为mm / dd / yyyy hh:mm:ss。 (月日)
我试图使用DateTime.parse这样:

I have DateTime variable in this format "dd/mm/yyyy hh:mm:ss". I would like to convert this to "mm/dd/yyyy hh:mm:ss". (Month before day) I tried to use DateTime.parse like this:

DateTime dt = new DateTime();
dt = dateTimePicker1.Value;            
dt = DateTime.Parse(dt.ToString(), "mm/dd/yyyy", null);

但它不工作。
我也尝试使用 DateTime.ParseExact(),但仍然没有发生。

but it wont work. I also try it with DateTime.ParseExact() but still nothing happened.

任何建议?

推荐答案

A DateTime 没有 em>隐式格式。它只是具有日期和时间值。

A DateTime doesn't have any implicit format. It just has date and time values.

只有格式化主题 适用于您尝试获取文字表示。如果你想得到它的字符串表示,你可以使用 .ToString()方法,如;

Formatting subject only applies when you try to get it's textual representation. If you wanna get string represetation of it, you can use .ToString() method like;

string s = dateTimePicker1.Value.ToString("MM/dd/yyyy hh:mm:ss", 
                                           CultureInfo.InvariantCulture);

请记住, mm 说明符是分钟, MM 说明符是几个月。另外 hh 说明符用于 12-小时时钟 HH 说明符是 24小时制 表示。您可以想使用 HH

And remember, mm specifier is for minutes, MM specifier is for months. Also hh specifier is for 12-hour clock and HH specifier is for 24-hour clock representations. You might wanna use HH instead.

我使用了 InvariantCulture 作为第二个参数,因为 / 具有特殊的含义,替换我当前文化或提供的文化日期或时间分隔符

I used InvariantCulture as a second parameter because / and : have special meaning as replace me current culture or supplied culture date or time separator.

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

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