Im trying to put current system time in YYMMDD format but having problem with day function.
is Day() which returned day of month in VB6 not available in VB.Net?
If I want to get the current day of month, how would I do that?

Dim dt As Date
Dim dtString As String

dtString = Year(now()) + Month(now()) + Day(now())

Dani AI

Generated

Short answer: the day-of-month is available in VB.NET, but the original expression was doing numeric addition instead of building a zero-padded string. and were on the right track using formatting; here is a clearer, safer explanation and a couple of concise options.

The recommended one-liner is to format the DateTime. It zero-pads month/day automatically, so you get exactly YYMMDD:

Dim yymmdd As String = DateTime.Now.ToString("yyMMdd")

If you prefer to build the pieces yourself (for clarity or special logic), use the DateTime properties and the D2 numeric format for padding, and use & for string concatenation to avoid accidental arithmetic:

Dim yy As String = DateTime.Now.Year.ToString().Substring(2)
Dim mm As String = DateTime.Now.Month.ToString("D2")
Dim dd As String = DateTime.Now.Day.ToString("D2")
Dim dtString As String = yy & mm & dd

Notes and tips:

  • Using + with integer Year/Month/Day will add numbers, not concatenate; that’s the likely bug in the original code. Enabling Option Strict On helps catch these issues (see Option Strict documentation).
  • For unambiguous, sortable dates prefer yyyyMMdd (four-digit year).
  • The idiomatic VB.NET approach is to use DateTime properties or formatting rather than the older VB6 helpers; see the DateTime.Day property and the custom date/time format strings for details: DateTime.Day and custom date/time format strings.

Recommended Answers

All 3 Replies

See If this helps.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        MsgBox(xDate)
        MsgBox(xDate("dd"))
    End Sub
    Private Function xDate(Optional ByVal selDateFormat As String = "MM.dd.yyyy") As String
        Return Date.Now.ToString(selDateFormat)
    End Function

Im trying to put current system time in YYMMDD format but having problem with day function.
is Day() which returned day of month in VB6 not available in VB.Net?
If I want to get the current day of month, how would I do that?

Dim dt As Date
Dim dtString As String

dtString = Year(now()) + Month(now()) + Day(now())

Use the below code so that u can get the system date

Dim d As String = Format(Today, "yy/MM/dd")
TextBox1.Text = d

Thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.