本文介绍了仅从文本框中检索月份值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问如何从文本框中获取月份值?出于某种原因,我不能将DateTimePicker用于此部分,因为这是客户的要求。代码如下:



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:

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





i只想要得到MM,我怎么能得到它?谢谢



i only want to get the "MM", how can i get it? Thanks

推荐答案


textBox1.Text = DateTime.Now.ToString("yyyy-MM");
DateTime dt =DateTime.ParseExact(textBox1.Text, "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture);
string month = dt.Month.ToString();



参考

[]

[]



或者,如果您需要将文本框文本设置为当前月份,请尝试使用属性 DateTime


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

Or if you need to set textbox text to current month, try with Month property of DateTime

textBox1.Text = DateTime.Now.Month.ToString();



[]


这篇关于仅从文本框中检索月份值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:54
查看更多