我想计算一个月中有多少天。显然,如果有a年,那么2月将有29天。
我有一些代码可以算出是否是a年,但我不知道它是否有用。
我试图计算当前年份并计算输入的当月日期;但是有一个错误,我不确定该怎么办。
Sub daysInMonth()
Console.WriteLine("Please Enter month you will like to calculate number of days: ")
Dim inputMonth As DateTime = Console.ReadLine()
Dim newMonth = DateAndTime.Month(inputMonth)
Dim current = Now()
Dim currentYear = (Year(current))
Dim febuaryLeapYear = System.DateTime.DaysInMonth(currentYear, newMonth)
End Sub
已解决
答案 0 :(得分:1)
我有此函数,可返回任何月份,任何年份的天数。希望有帮助
#top,
#bottom,
#right,
#left.
{
position: fixed;
left: 0;
right: 0;
height: 50%;
}
#top {
top: 0;
background-color: blue;
height: 20%;
}
#bottom {
bottom: 0;
background-color: green;
height: 80%
}
#right {
right: 0;
background-color: orange;
width: 20%;
}
#left {
left: 0;
background-color: red;
width: 80%;
}
答案 1 :(得分:0)
我认为在这种情况下,问题在于DaysInMonth要求月份以整数表示。因此,如果将inputMonth更改为Integer,则不会收到错误。
答案 2 :(得分:0)
Integer.TryParse(string,integerVariable)将检查字符串以查看是否可以将其转换为Integer。它将返回True或False,因此可以在If状态下使用。另外,它用字符串的整数表示填充integerVariable。
如果先前的条件为false,则If的AndAlso部分将永远不会执行。这称为短路。您的代码不会在AndAlso条件下比较数字,因为如果第一部分为假,它将永远不会执行。
Sub CalculateDaysInMonth() 'It is a bad idea to name your Sub the same as a .net method
Do 'The method will loop until the user enters correct data
'A more explicit message will help user to enter correct data
Console.WriteLine("Please enter a month by entering a number between 1 and 12")
Dim inputMonth As Integer
If Integer.TryParse(Console.ReadLine, inputMonth) AndAlso inputMonth > 0 AndAlso inputMonth < 13 Then
Dim currentYear As Integer = Now.Year
Dim numberOfDays As Integer = DateTime.DaysInMonth(currentYear, inputMonth)
'This is an interpolated string. If your version doesn't support this you can use String.Format
'String.Format("There are {0} in month {1} this year", numberOfDays, inputMonth)
Console.WriteLine($"There are {numberOfDays} in month {inputMonth} this year")
Return
End If
Loop
End Sub