代码之家  ›  专栏  ›  技术社区  ›  Diskdrive

如何将月份名称转换为int?

  •  1
  • Diskdrive  · 技术社区  · 16 年前

    我在一个存储过程中有一个名为Month的列,它以字符串名称(例如一月、二月、三月等)返回月份

    如果不使用多个select case语句,这是可能的吗?

    我想在家里做这件事。净额3.5

    4 回复  |  直到 14 年前
        1
  •  2
  •   Tahbaza    16 年前

    您可以使用DateTime。解析并获取日期的Month属性:

    string monthName = "January";
    DateTime.Parse(String.Format("{0} 1, 2000", monthName)).Month
    
        2
  •  0
  •   Community Mohan Dere    9 年前

    在SQL Server中执行此操作 SELECT DATEPART(month, GETDATE())

    也是这个问题的重复 How to parse a month name (string) to an integer for comparison in C#?

        3
  •  0
  •   Cyberpks    13 年前

    我最近遇到了这个问题(在客户端使用Javascript),在互联网上搜索了几次之后,我决定自己做。虽然我是用Javascript做的,但我还是会给你一个简单的答案。net解决方案。 Javascript首先:

    function convertToIntMonth(monText) {
    //Your arrays for the valid months for which you can throw exception (I did not do it) if the parameter is out of these 12.
            var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
            var monInt;
            for (i = 0; i < 12; i++) {
    //in this loop, we'll check for the match in parameter and array items.
                if (monText == monthNames[i])
                { 
    //If match is found, we'll return the +1 value of actual match (as month array's index goes from 0 to 11, and we require it from 1 to 12)
    monInt=i+1}
            }
            return monInt;
        }
    

    现在开始。NET(C#):还没有测试,但我希望你能找到逻辑

    public int convertToIntMonth(string monText) {
            string[] monthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
            string monInt=0;
            for (int i = 0; i < 12; i++) {
                if (monText == monthNames[i])
                { monInt=i+1}
            }
            return monInt;
        }
    
        4
  •  -1
  •   sarnold    16 年前

    您可以存储名称->数组中的数字映射(查看数组直到找到所需的数字)或散列(在散列中查找月份名称以获得数字——支持任意数量的语言和缩写也会带来好处)。我宁愿将信息存储在数据结构中而不是代码中,但这可能只是我自己的非理性行为。

    对于@roboshop的“听起来像是做了很多工作”,也许可以将其与平台支持的一行程序相比较,但如果您的客户要求您支持缩写词,如“Jan”、“Fe”、“Mar”,我认为使用数据结构可以提供很多额外的灵活性:

    months = { "ja" => 1, "jan" => 1, "january" => 1,
               "fe" => 2, "feb" => 2, "february" => 2,
               "ma" => 3, "mar" => 3, "march" => 3,
               "ap" => 4, "apr" => 4, "april" => 4,
               "my" => 5, "may" => 5,
               "jn" => 6, "jun" => 6, "june" => 6,
               "jl" => 7, "jul" => 7, "july" => 7,
               "au" => 8, "aug" => 8, "august" => 8,
               "se" => 9, "sep" => 9, "sept" => 9, "september" => 9,
               "oc" => 10, "oct" => 10, "october" => 10,
               "no" => 11, "nov" => 11, "november" => 11,
               "de" => 12, "dec" => 12, "december" => 12,
               "januar" => 1,
               "februar" => 2,
               "märz" => 3,
               "mai" => 5,
               "juni" => 6,
               "oktober" => 10,
               "dezember" => 12}
    
    puts "Month %s is %d" % [ARGV[0], months[ARGV[0].downcase]]
    

    当然,现在您必须知道用户想要的月份的本地化版本,但是支持所请求的缩写要容易得多。