【问题标题】:Returning an array of culture formatted dates C# MVC3返回一个文化格式化日期的数组 C# MVC3
【发布时间】:2012-12-15 16:58:23
【问题描述】:

我是编程新手,我正在尝试做一个练习,以各种格式将日期格式化为泰国文化,这就是我到目前为止的代码:

    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;
    }

这是附带的测试方法:

    [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);
    }

我在“controller.FormatAsSpecified(theDate, theCulture,formats[i]);”处的测试方法中遇到错误上面写着“参数 3,无法从 'string' 转换为 'string[]'” 我做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc-3 date culture


    【解决方案1】:

    改变

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

    到

    controller.FormatAsSpecified(theDate, theCulture, formats);
    

    我认为你应该更改代码

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

    到

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

    【讨论】:

    • 嗨,谢谢,但是当我这样做时,它会说,“无法将类型 'string[]' 隐式转换为 'string'”
    • @user1875797 你在哪一行得到这个错误。我猜,它在另一条线上。
    • 同上一行:"= controller.FormatAsSpecified(theDate, theCulture, formats)"
    • @user1875797 如果它对你有用,accepting 答案怎么样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多