vb.net 添加日期到日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44748692/
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
vb.net add days to a date
提问by Stephen Gellman
Dim DespatchDate As DateTime
Dim ReceiptDate As DateTime
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
ReceiptDate = DespatchDate.AddDays(60) 'Returns #5/6/2017 12:00:00 AM#
I would expect "1/9/2017" the 1st September 2017
我预计 2017 年 9 月 1 日是“1/9/2017”
I also tried
我也试过
ReceiptDate = DateAdd("d", 10, DespatchDate) which returned #3/17/2017 12:00:00 AM#
I must be doing something wrong but what?
我一定是做错了什么,但什么?
回答by Fabulous
The date literal in VB uses the US format regardless of where you are. Your code:
无论您身在何处,VB 中的日期文字都使用美国格式。您的代码:
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
is creating the date March 7 2017. Read more on date literals or use the constructor on the date class that makes it more apparent what you are doing.
正在创建日期 2017 年 3 月 7 日。阅读有关日期文字的更多信息或在日期类中使用构造函数,使您在做什么更清楚。
UpdateIn order to be sure what the date is and to avoid ambiguity, you can define it this way:
更新为了确定日期是什么并避免歧义,您可以这样定义它:
DespatchDate = New Date(2017, 7, 3) ' Without the time of day, 12AM is implied
DespatchDate = New Date(2017, 7, 3, 12, 34, 56) ' July 3 2017 12:34:56 PM
The good thing with this is you can see from intellisense while typing the code what each number means. And if someone else had written the code, you can invoke intellisense to see what each argument means. As you can see from what I highlighted in my screenshot, the time is in 24 hour format.
这样做的好处是您可以在输入代码时从智能感知中看到每个数字的含义。如果其他人编写了代码,您可以调用智能感知来查看每个参数的含义。从我在屏幕截图中突出显示的内容可以看出,时间采用 24 小时制。


