# Number of digit of day and month

Page 1 of 1

## 9 Replies - 7445 Views - Last Post: 30 January 2013 - 11:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=308353&amp;s=e34180fcfbe952a5af4c6371ff8eb603&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nighttrain

• D.I.C Regular

Reputation: 8
• Posts: 485
• 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

• D.I.C Regular

Reputation: 8
• Posts: 485
• 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

### #3 andrewsw

• blow up my boots

Reputation: 6544
• Posts: 26,525
• 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

### #4 nighttrain

• D.I.C Regular

Reputation: 8
• Posts: 485
• 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: ?

### #5 andrewsw

• blow up my boots

Reputation: 6544
• Posts: 26,525
• 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.

### #6 nighttrain

• D.I.C Regular

Reputation: 8
• Posts: 485
• 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

### #7 lar3ry

• Coding Geezer

Reputation: 314
• Posts: 1,296
• 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.

### #8 nighttrain

• D.I.C Regular

Reputation: 8
• Posts: 485
• 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?

### #9 nighttrain

• D.I.C Regular

Reputation: 8
• Posts: 485
• 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

### #10 lar3ry

• Coding Geezer

Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Number of digit of day and month

Posted 30 January 2013 - 11:24 AM

nighttrain, 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