Gopal
Gopal

Reputation: 11972

How to write a Date Code In vb.net?

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.

Upvotes: 0

Views: 8412

Answers (3)

awe
awe

Reputation: 22442

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

Upvotes: 1

ozczecho
ozczecho

Reputation: 8849

Something like:

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

Upvotes: 0

Dan Tao
Dan Tao

Reputation: 128317

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)

Upvotes: 4

Related Questions