tags:

views:

249

answers:

3

VB CODE.

dteFrom = Format(CDate(Year(Date) & "-" & Month(Date) & "-" & "01"), "yyyy-mm-dd")
dteTo = Format(CDate(Year(Date) & "-" & Month(Date) & "-" &  DaysInMonth(dteFrom)), "yyyy-mm-dd")

I copy this code in VB.NET, It showing error (of Date)

dteFrom = Format(CDate(Year(Of Date)() & "-" & Month(Of Date)() & "-" & "01"), "yyyy-mm-dd")
dteTo = Format(CDate(Year(Of Date)() & "-" & Month(Of Date)() & "-" & DaysInMonth(dteFrom)), "yyyy-mm-dd")

Can any one Help to solve this problem.

Need VB.Net Code.

A: 

Something like:

Dim dateString as String = string.format("{0:yyyy-MM-dd}", New DateTime(DateTime.Now.Year,DateTime.Now.Month, 1))
ozczecho
Those 'm's need to be capitalized; lower case will give you minutes.
Dan Tao
hahaha...yes ofcourse :-)
ozczecho
+3  A: 

It looks like you want two strings representing the first and last days of the current month. In that case you can do the following:

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)
Dan Tao
+1  A: 

This will be the closest to your original code:

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")

This will be a more ".NET" way to do it :

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)) )
awe
Note that the format string is uppercase **M** for month. Lowercase **m** is minutes. The VB6 function was "smart" and distinguished between minutes and month based on the logic in the pattern (looks like a date - month, looks like a time - minutes). In .NET you have to distinguish between upper and lowercase, just as for the String.Format function.
awe