vb.net 将月份名称转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3662292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:02:16  来源:igfitidea点击:

Converting month name to integer

vb.netdatetimedateinteger

提问by Mercurybullet

I did a quick search for this and was surprised not to find it anywhere.

我对此进行了快速搜索,但很惊讶没有在任何地方找到它。

Basically looking to convert full month names (January, September, etc) to the equivalent number that would be used in mm/dd/yyyy format.

基本上希望将完整的月份名称(一月、九月等)转换为以 mm/dd/yyyy 格式使用的等效数字。

I can put together my own array and pull it out accordingly, but there has to be a quick and straightforward method already. Right?

我可以将自己的数组放在一起并相应地将其拉出,但是必须已经有一种快速而直接的方法。对?

回答by bdukes

Dim monthName = "September"
Dim monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month

If you are basing this on user input, I think this is the cleanest (especially if you are expecting multiple cultures to use this). DateTime.ParseExactwill allow you to input any sort of input and translate it into a DateTime, then pluck off whatever part of it you care about.

如果您将此基于用户输入,我认为这是最干净的(特别是如果您希望多种文化使用它)。 DateTime.ParseExact将允许您输入任何类型的输入并将其转换为DateTime,然后摘下您关心的任何部分。

If, however, this isn't have on user input, I would have to suggest using some sort of static collection (whether a dictionary or an enum).

但是,如果这不是用户输入,我将不得不建议使用某种静态集合(无论是字典还是枚举)。

回答by Oded

You can use the format string MMMMfor the full name of the month.

您可以使用格式字符串MMMM作为月份的全名。

See custom DateTime format stringson MSDN.

请参阅MSDN 上的自定义日期时间格式字符串

Dim fullMonthName as DateTime
fullMonthName = DateTime.ParseExact("26 January 2010", "dd MMMM yyyy", 
                                           CultureInfo.InvariantCulture)

回答by user10067059

This is old but something I was looking for, just incase someone else comes looking I came up with an easy solution....

这是旧的但我一直在寻找的东西,以防万一其他人来找我想出了一个简单的解决方案....

Dim sM as String = "Jun"
Dim iM as Byte = 0

iM = Month(sM & " 1, 2020")

iM will equal 6

iM 将等于 6

回答by Amber Green

well in my case I used this trick

好吧,就我而言,我使用了这个技巧

  1. Create 2 combobox
  2. stuff combobox1 with the names of months
  3. stuff combobox2 with years
  1. 创建 2 个组合框
  2. 用月份名称填充combobox1
  3. 多年的东西组合框2

and then this

然后这个

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        MsgBox(System.DateTime.DaysInMonth(ComboBox2.Text,   ComboBox1.SelectedIndex + 1), MsgBoxStyle.Information)
    End Sub

回答by phreakocious

Call me crazy, but isn't this exactly what an enum is for? Maybe worth considering to keep the code as simple as possible. http://visualbasic.about.com/od/usingvbnet/a/enum01.htm

叫我疯了,但这不正是枚举的用途吗?也许值得考虑让代码尽可能简单。 http://visualbasic.about.com/od/usingvbnet/a/enum01.htm

回答by developer__c

This may sound long winded, why not use an IF Statement or Select Case.

这听起来可能有点啰嗦,为什么不使用 IF 语句或 Select Case。

If Month = "January" Then MonthNum = "1"
Else If Month = "February" Then .......