转换DD / MM / YYYY以MM / DD / YYYY [英] Convert dd/MM/yyyy to MM/dd/YYYY

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

问题描述

我需要转换28/08/2012,以 MM / DD / YYYY 格式,这意味着2012年8月28日。结果
口怎么能这样做呢?



我使用下面的代码,但它抛出异常给我。

  DateTime.ParseExact(28/08/2012,DDMMYYYY,CultureInfo.InvariantCulture)


解决方案

但它抛出异常给我




问题:



您日期中包含 / 分隔符(28/08/2012),你不给,在你的日期字符串格式(DDMMYYYY)。



解决方案:



这应该是DD / MM / YYYY



本办法

  DateTime.ParseExact(28/08/2012,DD / MM / YYYY,CultureInfo.InvariantCulture)
的ToString(MM / DD / YYYY,CultureInfo.InvariantCulture);



这样做,我们将获得一个DateTime对象与被转移到使用字符串<$您人口稠密日期后C $ C>的ToString()与所需的日期格式为:MM / DD / YYYY和可选的文化信息的CultureInfo .InvariantCulture


I need to convert "28/08/2012" to MM/dd/YYYY format that means "08/28/2012".
How can I do that?

I am using below code , but it threw exception to me.

DateTime.ParseExact("28/08/2012", "ddMMyyyy",  CultureInfo.InvariantCulture)

解决方案

but it threw exception to me

Problem:

Your date contains / seperator ("28/08/2012") and you are not giving that in your date string format ("ddMMyyyy").

Solution:

It should be "dd/MM/yyyy".

This way

DateTime.ParseExact("28/08/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture)
                        .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

After doing that we will receive a DateTime object with your populated dates which is transferred to string using .ToString() with desired date format "MM/dd/yyyy" and optional culture info CultureInfo.InvariantCulture.

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

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