9 Replies - 1170 Views - Last Post: 30 January 2013 - 11:24 AM Rate Topic: -----

#1 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 289
  • Joined: 22-September 10

Number of digit of day and month

Posted 21 January 2013 - 02:07 AM

Hello, Is there other possibility to represent number of day in forrmat xx, eg if day is 1 i would like to see 01, and the same with month, as you can see bellow i am checking Len(MyMonth) to add 0 if month has 1 number. I tried to use:
DatePart("mm", dt) but its not working.

 
Dim dt As Date = Now
            Dim MyYear As String = DatePart("yyyy", dt)
            Dim MyMonth As String = DatePart("m", dt)
            Dim MyDay As String = DatePart("d", dt)
            Dim MyFileName As String
            If Len(MyMonth) = 1 Then
                MyFileName = "Report1_" & MyYear & "-" & "0" & MyMonth & "-" & MyDay & ".xls"
            Else
                MyFileName = "Report1_" & MyYear & "-" & MyMonth & "-" & MyDay & ".xls"
            End If

            FName = OutDir & MyFileName



Is This A Good Question/Topic? 0
  • +

Replies To: Number of digit of day and month

#2 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 289
  • Joined: 22-September 10

Re: Number of digit of day and month

Posted 21 January 2013 - 02:27 AM

As i can see this gives me 2 digits number:
 Dim szFilename = Format(theDate.AddDays(-20), "dd")


the same with month.
but when i change to "d" or "m" it gives me full date?

This post has been edited by nighttrain: 21 January 2013 - 02:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3197
  • View blog
  • Posts: 10,724
  • Joined: 12-December 12

Re: Number of digit of day and month

Posted 21 January 2013 - 02:33 AM

Seems like a lot of hard work :)/>/>

Dim fileName As String = String.Format("Hello {0:yyyy-MM-dd}.xls", Now())


Custom date and time formats

This post has been edited by andrewsw: 21 January 2013 - 02:35 AM

Was This Post Helpful? 1
  • +
  • -

#4 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 289
  • Joined: 22-September 10

Re: Number of digit of day and month

Posted 21 January 2013 - 03:04 AM

ROFL, really fast way. What exactly means 0: ?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3197
  • View blog
  • Posts: 10,724
  • Joined: 12-December 12

Re: Number of digit of day and month

Posted 21 January 2013 - 03:18 AM

?String.Format("{0} {1}!", "Hello", "World")
"Hello World!"


so 0 refers to the first item, 1 to the second, after the first comma.
Was This Post Helpful? 1
  • +
  • -

#6 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 289
  • Joined: 22-September 10

Re: Number of digit of day and month

Posted 22 January 2013 - 05:15 AM

another question: How to retreive the week of year using the string format (2 digit number)?

This post has been edited by nighttrain: 22 January 2013 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Number of digit of day and month

Posted 22 January 2013 - 10:13 AM

Well, it's part of DateInterval, rather than DateTime. To get the week, you could use:
    Private Function GetWeek() As Integer
        Dim DateValue As DateTime = Now()
        Dim interval As DateInterval = DateInterval.WeekOfYear
        Dim FirstDayOfWeekValue As FirstDayOfWeek
        Dim FirstWeekOfYearValue As FirstWeekOfYear

        Return DateAndTime.DatePart(DateInterval.WeekOfYear, DateValue, FirstDayOfWeekValue, FirstWeekOfYearValue)

    End Function


You'll have to make it into two digits before putting it into your string. When you type in DateInterval, the editor will show you all the options for the different intervals, like Minute, Months, Quarter, etc.
Was This Post Helpful? 0
  • +
  • -

#8 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 289
  • Joined: 22-September 10

Re: Number of digit of day and month

Posted 23 January 2013 - 12:17 AM

but this gives me 1 digit format how to set 2?
Was This Post Helpful? 0
  • +
  • -

#9 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 289
  • Joined: 22-September 10

Re: Number of digit of day and month

Posted 30 January 2013 - 09:03 AM

i am still cant figure out how to display it in two digit format, could anyone help?

This post has been edited by nighttrain: 30 January 2013 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Number of digit of day and month

Posted 30 January 2013 - 11:24 AM

View Postnighttrain, on 30 January 2013 - 10:03 AM, said:

i am still cant figure out how to display it in two digit format, could anyone help?

As several folks have pointed out, use Format().
    Dim s as String = Format(5,"00")


The above line will output "05"

Here it is in the Function I gave you before...
    Private Function GetWeek() As String
        Dim DateValue As DateTime = Now()
        Dim interval As DateInterval = DateInterval.WeekOfYear
        Dim FirstDayOfWeekValue As FirstDayOfWeek
        Dim FirstWeekOfYearValue As FirstWeekOfYear
        Return Format(DateAndTime.DatePart(DateInterval.WeekOfYear, DateValue, FirstDayOfWeekValue, FirstWeekOfYearValue), "00")
    End Function


This post has been edited by lar3ry: 30 January 2013 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1