从字符串中获取值

时间:2023-02-05 21:26:43

I need help with a quick question I a string "07/10/2014" how can I get first the year "2014",second the month "10" ,third the day- "07" with out "/" only the values in VB.NET

我需要一个快速问题的帮助我是一个字符串“07/10/2014”如何获得第一年“2014”,第二个月“10”,第三个日期 - “07”没有“/”只有值在VB.NET中

Please show me the full way how to do it.

请告诉我完整的方法。

3 个解决方案

#1


1  

Have a look at this.

看看这个。

Dim MyDate As Date
MyDate = "07/10/2014"
MsgBox(Format(MyDate, "dd")) ' dd gives you day number
MsgBox(Format(MyDate, "MM")) ' MM gives you month number
MsgBox(Format(MyDate, "YYYY")) ' YYYY gives you year number

The full list of date fomatting string could be found here (MSDN)

日期fomatting字符串的完整列表可以在这里找到(MSDN)

UPDATE

Use following example to assign to a string variable

使用以下示例分配给字符串变量

Dim DayOfString As String DayOfString
DayOfString = Format(MyDate, "dd")

#2


2  

First declare it like this Dim x as Date = "07/10/2014". And to get the individual values use x.Day, x.Month and x.Year

首先声明它像Dim x as Date =“07/10/2014”。要获得单个值,请使用x.Day,x.Month和x.Year

#3


2  

Use the DateTime.Parse() method then use the return DateTime structure to extract the Month, Day, Year properties (similar to DJK's answer above).

使用DateTime.Parse()方法然后使用return DateTime结构来提取Month,Day,Year属性(类似于DJK上面的答案)。

If the thread's current culture is set to one that understands "mm/dd/yyyy" format, then the code can be as simple as:

如果线程的当前文化设置为理解“mm / dd / yyyy”格式的文化,则代码可以如下所示:

    Dim dt As DateTime = DateTime.Parse("07/15/2014")
    MessageBox.Show(String.Format("Month: {0}; Day: {1}; Year: {2}", dt.Month, dt.Day, dt.Year))

#1


1  

Have a look at this.

看看这个。

Dim MyDate As Date
MyDate = "07/10/2014"
MsgBox(Format(MyDate, "dd")) ' dd gives you day number
MsgBox(Format(MyDate, "MM")) ' MM gives you month number
MsgBox(Format(MyDate, "YYYY")) ' YYYY gives you year number

The full list of date fomatting string could be found here (MSDN)

日期fomatting字符串的完整列表可以在这里找到(MSDN)

UPDATE

Use following example to assign to a string variable

使用以下示例分配给字符串变量

Dim DayOfString As String DayOfString
DayOfString = Format(MyDate, "dd")

#2


2  

First declare it like this Dim x as Date = "07/10/2014". And to get the individual values use x.Day, x.Month and x.Year

首先声明它像Dim x as Date =“07/10/2014”。要获得单个值,请使用x.Day,x.Month和x.Year

#3


2  

Use the DateTime.Parse() method then use the return DateTime structure to extract the Month, Day, Year properties (similar to DJK's answer above).

使用DateTime.Parse()方法然后使用return DateTime结构来提取Month,Day,Year属性(类似于DJK上面的答案)。

If the thread's current culture is set to one that understands "mm/dd/yyyy" format, then the code can be as simple as:

如果线程的当前文化设置为理解“mm / dd / yyyy”格式的文化,则代码可以如下所示:

    Dim dt As DateTime = DateTime.Parse("07/15/2014")
    MessageBox.Show(String.Format("Month: {0}; Day: {1}; Year: {2}", dt.Month, dt.Day, dt.Year))