vb.net now.year

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2031666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:35:32  来源:igfitidea点击:

vb.net now.year

vb.netdate

提问by sdcsdc

i am new to vb.net and just wanted to clarify something. i have this code -

我是 vb.net 的新手,只是想澄清一些事情。我有这个代码 -

Dim i As Integer
For i = Now.Year To Now.Year

Next

for the code "For i = Now.Year To Now.Year", when the month changes to say May 2010, will the now.year to now.year change the 11 records to May 2011. or will it be may 2010 again?

对于代码“For i = Now.Year To Now.Year”,当月份更改为 2010 年 5 月时,从 now.year 到 now.year 是否会将 11 条记录更改为 2011 年 5 月,还是会再次更改为 2010 年 5 月?

回答by James

Now.Year only returns the year. So if you run it right now, it'll return 2010. If you run the same code next year, it'll return 2011.

Now.Year 只返回年份。所以如果你现在运行它,它会返回 2010。如果你明年运行相同的代码,它会返回 2011。

Ok, let me try to do this with my poor VB skills. :)

好吧,让我尝试用我糟糕的 VB 技能来做到这一点。:)

Dim last As String
last = ""
Try
    Dim i As Integer
    Dim j As Integer
    Dim time As DateTime = DateTime.Now
    i = Now.Year
        For j = 11 To 0
            If j < (Now.Month - 1) Then
                i = (Now.AddYears(1).Year)
            Else
                i = Now.Year
            End If
            last = (time.AddMonths(j)).ToString("MMMM") + " " + (i.ToString)
            DDL.Items.Add(last)
        Next
End Try

If you run this, it will populate the drop down list with the remaining months left in this year, then increase your Year integer, and add the months next year up until this month next year.

如果你运行它,它将用今年剩余的月份填充下拉列表,然后增加你的年份整数,并将明年的月份添加到明年的这个月。

At least I think this should work, but I'm not really up on my VB. It should give you an idea though.

至少我认为这应该可行,但我并没有真正了解我的 VB。它应该给你一个想法。

回答by jason

Based on your comment to this answer, try this:

根据您对此答案的评论,请尝试以下操作:

Dim j As Integer
Dim time As DateTime = DateTime.Now
For j = 0 To 11
    Dim s As String = time.AddMonths(j).ToString("MMMM yyyy")
    DDL.Items.Add(s)
Next

回答by Craig Gidney

I'm having a hard time understanding your question. 'Now' is a function which returns the current system time. If it is any month in 2010, Now.Year will return 2010. If it is any month in 2011, Now.Year will return 2011.

我很难理解你的问题。'Now' 是一个返回当前系统时间的函数。如果是 2010 年的任何月份,则 Now.Year 将返回 2010。如果是 2011 年的任何月份,则 Now.Year 将返回 2011。

But, and this is important, the result of 'Now' can change from call to call. If it is new year's eve, you can call Now.Year once and get 2010 then call it again and get 2011. This could happen in your for loop, because it calls Now.Year twice! Even worse, 'Now' depends on the current time set by the user. If the user backs up the date by a year at just the wrong timeyour loop might not run at all! (ie. because for i = 2010 to 2009 will not execute the body once)

但是,这很重要,“现在”的结果可以随电话而变化。如果是除夕,您可以调用 Now.Year 一次并获得 2010,然后再次调用它并获得 2011。这可能发生在您的 for 循环中,因为它两次调用 Now.Year!更糟糕的是,“现在”取决于用户设置的当前时间。如果用户在错误的时间将日期备份一年,您的循环可能根本无法运行!(即因为 for i = 2010 到 2009 不会执行一次主体)

I see a loop that should be an assignment of the current year to the variable i. You probably mean something totally different.

我看到一个循环,它应该是将当前年份分配给变量 i。你的意思可能完全不同。