如何在C#中查找上个月的数字 [英] How to find last month number in C#
本文介绍了如何在C#中查找上个月的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
i想要在c#中重温上个月的数字我试过但我只会在当月获得。
我尝试过:
string MonthNumber = DateTime.Now.Month.ToString();
i已经使用了上面的代码,它会给我当前的月号,我想要上个月的数字
也试过这个DateTime月= DateTime.Now.AddMonths(-1);
但它会给我一个日期我只想要月份数
hello
i want to just retrive the last month number in c# i have try but i will get only current month.
What I have tried:
string MonthNumber = DateTime.Now.Month.ToString();
i have used above code it will give me current month number i want last months number
also tried this DateTime month = DateTime.Now.AddMonths(-1);
but it will give me hole date i just want the month number
推荐答案
尝试:
int lastMonth = DateTime.Now.AddMonths(-1).Month;
您需要获取Month属性asAddMonths(-1)
将返回一个新的DateTime
实例,如下所示:
You need to get the Month Property asAddMonths(-1)
will return a newDateTime
instance, do it like:
DateTime LastMonth = DateTime.Now.AddMonths(-1);
Console.WriteLine(LastMonth.Month);
试试 -
Try-
string MonthNumber = DateTime.Now.AddMonths(-1).Month.ToString();
希望,它有帮助:)
Hope, it helps :)
这篇关于如何在C#中查找上个月的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文