我正在尝试将日期从一种形式转换为另一种形式 [英] Im trying to convert a date from one form to another

查看:143
本文介绍了我正在尝试将日期从一种形式转换为另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我是一个相当新的程序员,我喜欢尝试并创建自己的应用程序.

我正在尝试将日期转换为特定格式.
许多人可以不同地键入/写入日期,而我试图编写将所有这些日期转换为特定格式的代码.示例:

您可以按如下方式写今天的日期:

26/6 -2012(日/月/年)
120626(年/月/日)
12-06-26(年/月/日,带-")
2012年6月26日(日/月/年)

无论如何编写,我都想编写将其转换为的代码:
2012/06/26(年/月/日)

到目前为止,我已经提出了处理月份名称的语法:

Hi!

Im a fairly new programmer and i like to experiment and create my own applications.

Im trying to convert dates to a specific form.
Many people can type/write dates diffirently and im trying to write a code that converts all these dates to a specific form. Example:

You can write todays date as follows:

26/6 -2012(day/month/year)
120626(year/month/day)
12-06-26(year/month/day with "-")
26 june 2012(day/month/year)

No matter how this is written i want to write a code that converts it to:
2012/06/26(year/month/day)

So far i have come up with this syntax that handels the month names:

class Program
{
    static void Main(string[] args)
    {
        string strDate;
        Console.WriteLine("Please enter a date: ");
        strDate = Console.ReadLine();
        MonthsInArray month = new MonthsInArray(strDate);



        Console.ReadLine();
    }
}


还有另一类:


And the other class:

public MonthsInArray(string month)
{
    string strMonth;
    strMonth = month;
    Hashtable MyHash = new Hashtable();
    MyHash.Add("01","Januari");
    MyHash.Add("02","Februari");
    MyHash.Add("03","March");
    MyHash.Add("04","April");
    MyHash.Add("05","May");
    MyHash.Add("06","June");
    MyHash.Add("07","July");
    MyHash.Add("08","August");
    MyHash.Add("09","September");
    MyHash.Add("10","Oktober");
    MyHash.Add("11","November");
    MyHash.Add("12","December");


    foreach (string strMonthIndex in MyHash.Keys)
    {
        string strMonthName = (string)MyHash[strMonthIndex];

        if (strMonth == strMonthName)
        {
            Console.WriteLine(strMonthIndex);
        }

    }



在这种卡住之后,由于无论日期如何写入,日期都将保存在一个字符串变量中.虽然这是我希望的样子,但我确实欢迎其他解决方案ASLONG,因为它提供了具有ONE变量soloution的解决方案.

我真的很感谢我能在这里得到的任何帮助!

问候
Peter



After this im kind of stuck, since the date is to be saved in ONE string variable no matter how it is written. Although this is what i would like it to be like but i do welcome other soloutions ASLONG as a soloution with the ONE variable soloution is presented.

Im really thankfull for any help that i can get here!

Regards
Peter

推荐答案

为什么不使用
DateTime

尝试根据期望的格式来解析传递的字符串,并且将其存储为DateTime,然后可以以您喜欢的格式输出.

Try to parse the passed string according to the expected formats and store it as a DateTime, then you can output in your preferred format.


请参阅我对问题的评论.一切已经为您完成,只需查看类型System.DateTime http://msdn. microsoft.com/en-us/library/system.datetime.aspx [ http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ](标准格式字符串),
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ](自定义格式字符串).



回应后续讨论.首先,您的代码示例错误.更重要的是,您的问题通常无法解决.基本上,这是您应该使用的内容:
Please see my comment to the question. Everything is already done for you, just look at the type System.DateTime, http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

To get a time value from string, you can use one of parse methods, please read about them. To write it in a string in required format, use one of System.DateTime.ToString methods. The particular format is defined by culture (see ToString(IFormatProvider), ToString(String, IFormatProvider), and CultureInfo which implements IFormatProvider) and/or string format. For formats, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^] (standard format strings),
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^] (custom format strings).



To respond to the follow-up discussion. First, you code sample is wrong. More importantly, your problem cannot be solved in general case. Here is what you should use, basically:
string dateString = "03-04-2012";
System.DateTime dateMonthDayYear = System.DateTime.ParseExact(
    dateString,
    new string[] { "MM-dd-yyyy" },
    null, System.Globalization.DateTimeStyles.None);
System.DateTime dateDayMonthYear = System.DateTime.ParseExact(
    dateString,
    new string[] { "dd-MM-yyyy" },
    null, System.Globalization.DateTimeStyles.None);
System.DateTime dateAmbiguous = System.DateTime.ParseExact(
    dateString, new string[] { "dd-MM-yyyy", "MM-dd-yyyy" },
    null, System.Globalization.DateTimeStyles.None);


日期字符串"03-04-2012"的前两个解释是明确的,最后一个是模棱两可的,因为如果没有先验的假设,您将无法确定"03"是一个月还是一天.但是,如果您输入的是"29-04-2012"或"04-29-2012",则第一个调用ParseExact的人将引发异常(使用TryParseTryParseExact可以正常工作) ,如果您愿意的话),但第三个电话会给您明确的结果;这三种情况都将以这种方式工作,只是因为"29"不能是月份数字.

这样,无论您如何努力,都不能允许用户在不指定解析规则的情况下以任意格式输入日期.

实际上,您可以使用固定的自定义或标准格式,或者作为完美的解决方案,使用带有日期时间选择器的某些UI,例如System.Windows.Forms.DateTimePickerhttp://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.aspx [


First two interpretation of the date string "03-04-2012" are unambiguous, by the last one is ambiguous, because, without a priory assumptions, you cannot tell if "03" is a month or a day. But, if your input would be "29-04-2012" or "04-29-2012", one of the first to calls to ParseExact will throw an exception (use TryParse, TryParseExact to work without exceptions, if you want), but the third call will give you an unambiguous result; all three cases will work this way just because "29" cannot be a month number.

This way, you cannot allow the user to enter date in arbitrary format without specification of parsing rules, no matter how hard you try.

Practically, you can used fixed custom or standard format, or, as a perfect solution, some UI with date-time picker like System.Windows.Forms.DateTimePicker, http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.aspx[^].

Enjoy,

—SA


这篇关于我正在尝试将日期从一种形式转换为另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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