简体   繁体   English

返回一组文化格式化日期C#MVC3

[英]Returning an array of culture formatted dates C# MVC3

I'm new to programming and trying to do an exercise that formats a date to the Thai culture in a variety of formats this is what I have for my code so far: 我是编程的新手,并尝试以各种格式为泰国文化格式化日期,这是我迄今为止对我的代码所拥有的:

    public String[] FormatAsSpecified(DateTime theDate, String theCulture, String[] formats)
    {
        String[] dateResults = new String[formats.Length];

        CultureInfo culture = CultureInfo.GetCultureInfo(theCulture);

        for (int i = 0; i < formats.Length; i++)
        {
            String culture_formatted_date = theDate.ToString(formats[i], culture);
            dateResults[i] = culture_formatted_date;
        }

        return dateResults;
    }

This is the test method that goes with it: 这是随之而来的测试方法:

    [TestMethod]
    public void FormatAsSpecifiedReturnsDateLiteralsInSpecifiedFormatForAllStandardFormatStrings()
    {
        //Arrange
        var controller = new DateController();

        var theDate = new DateTime(2014, 2, 14, 9, 15, 32, 376);
        String theCulture = "th-TH";
        // Array of all supported standard date and time format specifiers.
        String[] formats
            = { "d", "D", "f", "F", "g", "G", "m", "o", "r", "s", "t", "T", "u", "U", "Y" };
        //Corresponding date literals for the standard Thai regional settings
        String[] expectedResults
            = {"14/2/2557"
                , "14 กุมภาพันธ์ 2557"
                , "14 กุมภาพันธ์ 2557 9:15"
                , "14 กุมภาพันธ์ 2557 9:15:32"
                , "14/2/2557 9:15"
                , "14/2/2557 9:15:32"
                , "14 กุมภาพันธ์"
                , "2014-02-14T09:15:32.3760000"
                , "Fri, 14 Feb 2014 09:15:32 GMT" 
                , "2014-02-14T09:15:32"
                , "9:15"
                , "9:15:32"
                , "2014-02-14 09:15:32Z"
                , "วันศุกร์ที่ 14 กุมภาพันธ์ 2014 9:15:32"
                , "กุมภาพันธ์ 2557"};

        //Act
        String[] actualResults = new String[15];
        for (int i = 0; i < formats.Length; i++)
        {
            actualResults[i]
                = controller.FormatAsSpecified(theDate, theCulture, formats[i]);
        }

        //Assert
        CollectionAssert.AreEqual(expectedResults, actualResults);
    }

I get an error in the test method at 'controller.FormatAsSpecified(theDate, theCulture, formats[i]);' 我在'controller.FormatAsSpecified(theDate,theCulture,formats [i]);'的测试方法中出错了。 that says "Argument 3, cannot convert from 'string' to 'string[]'" What am I doing wrong? 说“参数3,不能从'字符串'转换为'字符串[]'”我做错了什么?

Change 更改

controller.FormatAsSpecified(theDate, theCulture, formats[i]);

to

controller.FormatAsSpecified(theDate, theCulture, formats);

I think you should change the code from 我认为你应该改变代码

//Act
String[] actualResults = new String[15];
for (int i = 0; i < formats.Length; i++)
{
    actualResults[i]
        = controller.FormatAsSpecified(theDate, theCulture, formats[i]);
}

to

 String[] actualResults = 
           controller.FormatAsSpecified(theDate, theCulture, formats);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM