Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, may i ask how to get only month value from a textbox? For some reason i cant use DateTimePicker for this part as it is a requirement from customer. The codes is as below:

C#
textBox1.Text = DateTime.Now.ToString("yyyy-MM");


i only want to get the "MM", how can i get it? Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 18-May-14 23:48pm    
What's wrong with just reading standard MSDN documentation?
—SA

Parse it as System.DateTime and get month from the result. This is what you need:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131044.aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetime.month.aspx[^].

See other methods named System.DateTime.Parse, System.DateTime.ParseExact, System.DateTime.TryParse and System.DateTime.TryParseExact.

—SA
 
Share this answer
 
if you need to get month from textbox with format like "yyyy-MM"
C#
textBox1.Text = DateTime.Now.ToString("yyyy-MM");
DateTime dt =DateTime.ParseExact(textBox1.Text, "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture);
string month = dt.Month.ToString();

References
DateTime.ParseExact Method[^]
DateTime.Month Property[^]

Or if you need to set textbox text to current month, try with Month property of DateTime
C#
textBox1.Text = DateTime.Now.Month.ToString();

C# DateTime.Month[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900