将字符串转换为固定日期时间格式C# [英] Convert String To Fixed DateTime Format C#

查看:94
本文介绍了将字符串转换为固定日期时间格式C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,

Hello Everyone,

我想转换将从用户那里获得输入的字符串(以任何日期时间格式),并且必须始终将其转换为固定格式为
" yyyy-MM-dd HH:mm:ss"使用c#。  因此,我可以从输入字符串中提取日期时间格式,但无法将其转换为
" yyyy-MM-dd HH:mm: SS" 格式。

I want to convert the string which will get input from user(in any date time formats) and have to convert always into fixed format as "yyyy-MM-dd HH:mm:ss" using c#. So, I am able to extract the date-time format from the input string but not able to convert it into "yyyy-MM-dd HH:mm:ss" format.

以下是从输入字符串中查找日期时间格式的代码,

Below is the code to find the date-time format from the input string,

string[] formats = {"M/d/yyyy", "MM/dd/yyyy",                                    
                            "d/M/yyyy", "dd/MM/yyyy", 
                            "yyyy/M/d", "yyyy/MM/dd",
                            "M-d-yyyy", "MM-dd-yyyy",                                    
                            "d-M-yyyy", "dd-MM-yyyy", 
                            "yyyy-M-d", "yyyy-MM-dd",
                            "M.d.yyyy", "MM.dd.yyyy",                                    
                            "d.M.yyyy", "dd.MM.yyyy", 
                            "yyyy.M.d", "yyyy.MM.dd",
                            "M,d,yyyy", "MM,dd,yyyy",                                    
                            "d,M,yyyy", "dd,MM,yyyy", 
                            "yyyy,M,d", "yyyy,MM,dd",
                            "M d yyyy", "MM dd yyyy",                                    
                            "d M yyyy", "dd MM yyyy", 
                            "yyyy M d", "yyyy MM dd"
                           };

            DateTime dateValue;

            foreach (string dateStringFormat in formats)
            {
                if (DateTime.TryParseExact(textBox2.Text.Trim(), dateStringFormat,
                                           CultureInfo.InvariantCulture,
                                           DateTimeStyles.None,
                                           out dateValue))
                    MessageBox.Show(dateStringFormat);
            }

请做好。

问候

rushuMT

推荐答案

首先:说出您的代码的作用。您不希望将任何输入格式转换为固定格式,您需要DateTime值。格式化是数据表示而不是数据类型。您应该只关心输入和
输出图层的格式。在代码中使用DateTime。

First of all: Say what your code does. You don't want to convert any input format to a fixed format, you want a DateTime value. Formatting is data representation and not the data type. You should only care about the formatting right at the input and output layers. Use DateTime in your code.

无法使用通用方法。原因是'04 / 05/2017'五月四日或四月五日? 

The generic approach is not possible. Cause is '04/05/2017' the fourth of May or the fifth of April? 

处理输入字符串时,只需定义一个允许格式的明确子集。在这种情况下,当您阅读TextBox时,它更简单。使用用户区域和日期设置。并将它们强制为

输入格式
。当用户想要不同的格式时,让他选择。存储此格式并强制执行。

When you deal with input strings just define a unambiguous sub-set of allowed formats. In this case as you read a TextBox it's much simpler. Use the users regional and date settings. And enforce them as input format. When the user wants a different format, then let him choose. Store this format and also enforce it.

对于您的实际问题:它是关于格式化DateTime值。

For your actual problem: It's about formatting a DateTime value.

在您的情况下,它很简单:

In your case its a simple:

MessageBox.Show(dateValue.ToString("yyyy-MM-dd HH:mm:ss"));

并且用于测试:

    public class Program
    {
        static void Main(string[] args)
        {

            DateTime dateValue = DateTime.Now;
            Console.WriteLine(dateValue.ToString("yyyy-MM-dd HH:mm:ss"));
            CultureInfo[] cultures = { new CultureInfo("en-US"),
                                 new CultureInfo("fr-FR"),
                                 new CultureInfo("it-IT"),
                                 new CultureInfo("de-DE") };
            foreach (CultureInfo culture in cultures)
                Console.WriteLine("{0}: {1}", culture.Name, dateValue.ToString(culture));
            Console.WriteLine(dateValue.ToString());
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }


这篇关于将字符串转换为固定日期时间格式C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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