如何将日期格式从mm / dd / yyyy更改为dd / mm / yyyy [英] How to change date format from mm/dd/yyyy to dd/mm/yyyy

查看:110
本文介绍了如何将日期格式从mm / dd / yyyy更改为dd / mm / yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。





请问如何将日期格式从mm / dd / yyyy更改为dd / mm / yyyy,i试过这些:



Hello everyone


Please how can i change date format from mm/dd/yyyy to dd/mm/yyyy, i have tried these:

DateTime res = DateTime.Parse( value, CultureInfo.GetCultureInfo( "en-gb") );











and

IFormatProvider culture = new CultureInfo( "en-gb", true );
                                            DateTime    date = DateTime.Parse( value , culture );





但仍然无法完成它。



变量值是一个字符串变量,包含这种格式的日期:dd mm yyyy,我希望能够以这种格式显示它,但是日期和res变量的格式为:mm / dd / yyyy。无论如何我仍然可以用这种格式显示它:dd mm yyyy?在此先感谢。



But still could not get it done.

the variable value is a string variable that contains date in this format: dd mm yyyy, i want to be able to display it in that format, but what is in date and res variables is in this format: mm/dd/yyyy. is there anyway i can still display it in this format: dd mm yyyy? Thanks in advance.

推荐答案

试试这个:

Try this:
using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string value = "25 06 2014";
        string format = "dd mm yyyy";
        DateTime result;
        if (DateTime.TryParseExact(value,format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result)){

            Console.WriteLine(result.ToString(format));
        } else {
            Console.WriteLine("Date invalid");
        }
    }
}


改为使用ParseExact:

Use ParseExact instead:
string value = "03 02 2014";
DateTime res = DateTime.ParseExact(value, "dd MM yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(res.ToString("MM/dd/yyyy"));


继续(正确)解决方案1和2要注意的一个关键点
Further to (the correct) Solutions 1 and 2 a key point to note
引用:

日期和res变量的格式为:mm / dd / yyyy

what is in date and res variables is in this format: mm/dd/yyyy

不是这种情况。



日期 res 属于 DateTime 所以没有格式的概念,如果在将它们显示为文本时省略了任何格式,或者在查看局部窗口中的值时,您将看到显示的值在您机器的默认文化中 - 通过外观看美国格式

is not the case.

date and res are of type DateTime so there is no concept of "format", if you have omitted any formatting when displaying them as text, or when looking at the values in the Locals window you will see the values displayed in the default culture for your machine - US format by the look of it


这篇关于如何将日期格式从mm / dd / yyyy更改为dd / mm / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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