Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vb.net中编写日期代码?_Vb.net - Fatal编程技术网

如何在vb.net中编写日期代码?

如何在vb.net中编写日期代码?,vb.net,Vb.net,VB代码 dteFrom = Format(CDate(Year(Date) & "-" & Month(Date) & "-" & "01"), "yyyy-mm-dd") dteTo = Format(CDate(Year(Date) & "-" & Month(Date) & "-" & DaysInMonth(dteFrom)), "yyyy-mm-dd") 我在VB.NET中复制此代码,它显示错误(日期) 谁能帮助解决

VB代码

dteFrom = Format(CDate(Year(Date) & "-" & Month(Date) & "-" & "01"), "yyyy-mm-dd")
dteTo = Format(CDate(Year(Date) & "-" & Month(Date) & "-" &  DaysInMonth(dteFrom)), "yyyy-mm-dd")
我在VB.NET中复制此代码,它显示错误(日期)

谁能帮助解决这个问题呢

需要VB.Net代码。

类似于:

Dim dateString as String = string.format("{0:yyyy-MM-dd}", New DateTime(DateTime.Now.Year,DateTime.Now.Month, 1))
比如:

Dim dateString as String = string.format("{0:yyyy-MM-dd}", New DateTime(DateTime.Now.Year,DateTime.Now.Month, 1))

看起来您需要两个字符串来表示当前月份的第一天和最后一天。在这种情况下,您可以执行以下操作:

Dim today As Date = Date.Today
Dim desiredFormat As String = "yyyy-MM-dd"

Dim fromDate As Date = New Date(today.Year, today.Month, 1)
Dim dteFrom As String = fromDate.ToString(desiredFormat)

Dim toDate As Date = fromDate.AddDays(Date.DaysInMonth(today.Year, today.Month) - 1)
Dim dteTo As String = toDate.ToString(desiredFormat)

看起来您需要两个字符串来表示当前月份的第一天和最后一天。在这种情况下,您可以执行以下操作:

Dim today As Date = Date.Today
Dim desiredFormat As String = "yyyy-MM-dd"

Dim fromDate As Date = New Date(today.Year, today.Month, 1)
Dim dteFrom As String = fromDate.ToString(desiredFormat)

Dim toDate As Date = fromDate.AddDays(Date.DaysInMonth(today.Year, today.Month) - 1)
Dim dteTo As String = toDate.ToString(desiredFormat)

这将是最接近您的原始代码:

dteFrom = Format(CDate(Year(Date.Today) & "-" & Month(Date.Today) & "-" & "01"), "yyyy-MM-dd")
dteTo = Format(CDate(Year(Date.Today) & "-" & Month(Date.Today) & "-" & Date.DaysInMonth(Year(Date.Today), Month(Date.Today))), "yyyy-MM-dd")
这将是一种更为“.NET”的方法:

dteFrom = String.Format("{0:yyyy-MM}-01", Date.Today)
dteTo = String.Format("{0:yyyy-MM}-{1:00}", Date.Today, Date.DaysInMonth(Year(Date.Today), Month(Date.Today)) )

这将是最接近您的原始代码:

dteFrom = Format(CDate(Year(Date.Today) & "-" & Month(Date.Today) & "-" & "01"), "yyyy-MM-dd")
dteTo = Format(CDate(Year(Date.Today) & "-" & Month(Date.Today) & "-" & Date.DaysInMonth(Year(Date.Today), Month(Date.Today))), "yyyy-MM-dd")
这将是一种更为“.NET”的方法:

dteFrom = String.Format("{0:yyyy-MM}-01", Date.Today)
dteTo = String.Format("{0:yyyy-MM}-{1:00}", Date.Today, Date.DaysInMonth(Year(Date.Today), Month(Date.Today)) )

这些m需要资本化;小写会给你几分钟的时间。这些m需要大写;小写表示分钟。请注意,格式字符串是大写的M表示月份。小写的m是分钟。VB6函数是“智能”的,根据模式中的逻辑(看起来像日期-月份,看起来像时间-分钟)区分分钟和月份。在.NET中,您必须区分大小写,就像String.Format函数一样。请注意,格式字符串是大写的M表示月份。小写的m是分钟。VB6函数是“智能”的,根据模式中的逻辑(看起来像日期-月份,看起来像时间-分钟)区分分钟和月份。在.NET中,您必须区分大小写,就像String.Format函数一样。