开发者

How do I grab this integer from a date in VB?

开发者 https://www.devze.com 2023-04-13 03:20 出处:网络
txtDate = 3/7/1994 Basically I want my button to calculate the \'month\' digit (in this case 7) and display it into txtMonth.
txtDate = 3/7/1994

Basically I want my button to calculate the 'month' digit (in this case 7) and display it into txtMonth.

What is the simplest way to do this?

Note the date will come from a user's input.

By the way, it's for Visual Basic! If you could actually explain it instead of telling me what to do, that would be great!

Found the code:

Dim theDate As Date
Dim theMonth As Intege开发者_开发百科r
    theDate = txtDateOfBirth.Text
    theMonth = Month(theDate)
    txtMonth.Text = theMonth

Cheers!


Split 3/7/1994 it on slashes, pick the second value from the splits and then assign it to txtMonth. This should be doable in whatever your programming language is :)

In C#

string[] splits = dateValue.Split("/".ToCharArray());

txtMonth = splits[1];

Supposing dateValue = "3/7/1994"

But do specify your programming language.


Split the string into an array (on "/"), the second item (1), will be your month.


convert your date variable to a datetime then use the format of datetime (C# code):

DateTime dt;
bool isValid = DateTime.TryParse(txtDate, out dt);
if (isValid)
    dt.ToString("MM");
0

精彩评论

暂无评论...
验证码 换一张
取 消