C# 如何在vb.net中将MMM格式的月份转换为MM日期?

C# 如何在vb.net中将MMM格式的月份转换为MM日期?,c#,asp.net,vb.net,date,datetime,C#,Asp.net,Vb.net,Date,Datetime,我需要从缩写中获取月份的编号: 我有这个: Dim monthname as string = "Jun" 我需要得到这个: Dim monthnumber as integer = 6 我怎么能得到这个?谢谢您可以尝试使用: DateTime.ParseExact("Jun", "MMM", CultureInfo.InvariantCulture) C# VB: 您可以尝试使用以下方法: DateTime.ParseExact("Jun", "MMM", CultureInfo.

我需要从缩写中获取月份的编号:

我有这个:

Dim  monthname as string =  "Jun"
我需要得到这个:

Dim monthnumber as integer =  6

我怎么能得到这个?谢谢

您可以尝试使用:

DateTime.ParseExact("Jun", "MMM", CultureInfo.InvariantCulture)
C#

VB:


您可以尝试使用以下方法:

DateTime.ParseExact("Jun", "MMM", CultureInfo.InvariantCulture)
C#

VB:


您可以使用
ParseExact
执行以下操作:

Dim  monthname as string =  "Jun"
Dim monthNumber as integer = DateTime.ParseExact(monthname , "MMM", CultureInfo.CurrentCulture).Month
或者,创建字典:

Dim months = New Dictionary(Of String, Integer) From
{{1, "Jan"}, {2, "Feb"}}, ...

Dim monthNumber = months(monthname)

您可以使用
ParseExact
执行以下操作:

Dim  monthname as string =  "Jun"
Dim monthNumber as integer = DateTime.ParseExact(monthname , "MMM", CultureInfo.CurrentCulture).Month
或者,创建字典:

Dim months = New Dictionary(Of String, Integer) From
{{1, "Jan"}, {2, "Feb"}}, ...

Dim monthNumber = months(monthname)

我测试代码是否正常工作。请同时导入System.Globalization

 Imports System
 Imports System.Globalization

Public Class Test
    Public Shared Sub Main()
        Dim  monthname as string =  "Jun"
        Dim monthnumber as integer = DateTime.ParseExact(monthName, "MMM", CultureIn

fo.CurrentCulture).Month 
        System.Console.WriteLine(monthnumber)
    End Sub
End Class

我测试代码是否正常工作。请同时导入System.Globalization

 Imports System
 Imports System.Globalization

Public Class Test
    Public Shared Sub Main()
        Dim  monthname as string =  "Jun"
        Dim monthnumber as integer = DateTime.ParseExact(monthName, "MMM", CultureIn

fo.CurrentCulture).Month 
        System.Console.WriteLine(monthnumber)
    End Sub
End Class

我只需要将字符串month name转换为month number,但不需要传递完整的date。您能展示一下目前如何将字符串转换为datetime吗?下面的两个答案都引用了我上面发布的问题!)@ScottPerham确实很相似,但阅读问题,OP似乎有一些困难将其从完整语法修改为短月语法,这就是重复的原因。我只需要将字符串“月名”转换为“月号”,但不需要传递完整的日期。您能展示一下目前如何将字符串转换为日期时间吗?下面的两个答案都引用了我在上面发布的问题!:)@ScottPerham这确实很相似,但阅读问题,OP似乎有一些困难将其从完整语法修改为短月语法,这就是重复的原因。您的第一个代码对我来说非常完美,谢谢您的帮助您的第一个代码对我来说非常完美,谢谢您的帮助