How many minutes past this month

  • Thread starter Thread starter Jozef Jarosciak
  • Start date Start date
J

Jozef Jarosciak

I need the function to tell me how many minutes past so far this month.
 
i guess, this is it...

DateDiff(DateInterval.Minute, CType("01/" & Date.Now.Month & "/" &
Date.Now.Year, System.DateTime), Now())
 
Dim minthismonth As Integer = DateDiff(DateInterval.Minute,
CType(("01/" & Date.Now.Month & "/" & Date.Now.Year), System.DateTime),
Now())

is showing result of : 272895

Which cannot be the truth. Today is 15th, so lets round it:
15(days) x 24(hours) x 60(minutes) = 21600 minutes

Result shouldnt be 272895, but little less than 21600.
Can someone see what is wrong with this code?

Thanks
Jozef Jarosciak
 
Jozef Jarosciak said:
Dim minthismonth As Integer = DateDiff(DateInterval.Minute,
CType(("01/" & Date.Now.Month & "/" & Date.Now.Year), System.DateTime),
Now())

is showing result of : 272895

Which cannot be the truth. Today is 15th, so lets round it:
15(days) x 24(hours) x 60(minutes) = 21600 minutes

Result shouldnt be 272895, but little less than 21600.
Can someone see what is wrong with this code?

Thanks
Jozef Jarosciak

Looks like it should have been:

CType((Date.Now.Month & "/01/" & Date.Now.Year), System.DateTime), Now())

It was probably calculating from January 7th to now.
 
Rothariger said:
i guess, this is it...

DateDiff(DateInterval.Minute, CType("01/" & Date.Now.Month & "/" &
Date.Now.Year, System.DateTime), Now())

Sometimes the answer to the question "Why???" is more interesting than
the answer to the problem.
 
then you must to check the regional settings, because i have configured
dd/mm/yyyy and it does work good for me...

salute!
 
Jozef said:
Result shouldnt be 272895, but little less than 21600.
Can someone see what is wrong with this code?

ALWAYS use non-ambiguous date formats. Otherwise you will be plagued by
problems if you ever run your code with a different date format.

When storing them in a string, always format them in ISO8601 (yyyy-MM-dd)
format:

\\\
DateDiff(DateInterval.Minute, CType((Date.Now.Year & "-" & Date.Now.Month &
"-01"), System.DateTime), Now())
///

(Though strictly speaking the month should have a leading zero for January
to September).

Even better, don't use strings to represent the date at all:

\\\
DateDiff(DateInterval.Minute, New Date(Date.Now.Year, Date.Now.Month, 1),
Now())
///

Hope that helps,
 
Jozef Jarosciak said:
I need the function to tell me how many minutes past so far this month.

Dim n As Date = Now

MsgBox(n.Subtract(New Date(n.Year, n.Month, 1)).TotalMinutes)


Armin
 
Jozef Jarosciak said:
Dim minthismonth As Integer = DateDiff(DateInterval.Minute,
CType(("01/" & Date.Now.Month & "/" & Date.Now.Year), System.DateTime),
Now())

is showing result of : 272895

Which cannot be the truth. Today is 15th, so lets round it:
15(days) x 24(hours) x 60(minutes) = 21600 minutes

Result shouldnt be 272895, but little less than 21600.
Can someone see what is wrong with this code?

Thanks
Jozef Jarosciak

Why use a string for DateDiff?


Public Function MinutesThisMonth() As Long
Return DateDiff( _
DateInterval.Minute, _
New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), _
DateTime.Now _
)
End Function

HTH,
Mythran
 
Rothariger,
I get 21039.65749464 with Armin's code, which is what I would expect, as 15
* 24 * 60 is 21600, its about 10 hours to midnight here...

Hope this helps
Jay

| hi, dont work for me...
|
| it returns:
| 1054283984.0714235
|
| "Armin Zingler" wrote:
|
| >
| > >I need the function to tell me how many minutes past so far this month.
| >
| > Dim n As Date = Now
| >
| > MsgBox(n.Subtract(New Date(n.Year, n.Month, 1)).TotalMinutes)
| >
| >
| > Armin
| >
| >
 
Mythran,
Of course if you call your function at Midnight on Dec 31st you may get
really interesting results.

The first Now, may return 31 Dec 2005, while the second & third Now return 1
Jan 2006, which would cause an entire years worth of minutes to be added
in...

I find Armin's example of storing Now in a separate variable to be much more
reliable & less likely to cause obscure bugs...

Just a thought
Jay

|
| | > Dim minthismonth As Integer = DateDiff(DateInterval.Minute,
| > CType(("01/" & Date.Now.Month & "/" & Date.Now.Year), System.DateTime),
| > Now())
| >
| > is showing result of : 272895
| >
| > Which cannot be the truth. Today is 15th, so lets round it:
| > 15(days) x 24(hours) x 60(minutes) = 21600 minutes
| >
| > Result shouldnt be 272895, but little less than 21600.
| > Can someone see what is wrong with this code?
| >
| > Thanks
| > Jozef Jarosciak
| >
|
| Why use a string for DateDiff?
|
|
| Public Function MinutesThisMonth() As Long
| Return DateDiff( _
| DateInterval.Minute, _
| New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), _
| DateTime.Now _
| )
| End Function
|
| HTH,
| Mythran
|
 
Doh!

I should add: any month end may cause a problem, as the first Now may return
12/31 while the second may return 1/1...

Jay

| Mythran,
| Of course if you call your function at Midnight on Dec 31st you may get
| really interesting results.
|
| The first Now, may return 31 Dec 2005, while the second & third Now return
1
| Jan 2006, which would cause an entire years worth of minutes to be added
| in...
|
| I find Armin's example of storing Now in a separate variable to be much
more
| reliable & less likely to cause obscure bugs...
|
| Just a thought
| Jay
|
| ||
|| || > Dim minthismonth As Integer = DateDiff(DateInterval.Minute,
|| > CType(("01/" & Date.Now.Month & "/" & Date.Now.Year), System.DateTime),
|| > Now())
|| >
|| > is showing result of : 272895
|| >
|| > Which cannot be the truth. Today is 15th, so lets round it:
|| > 15(days) x 24(hours) x 60(minutes) = 21600 minutes
|| >
|| > Result shouldnt be 272895, but little less than 21600.
|| > Can someone see what is wrong with this code?
|| >
|| > Thanks
|| > Jozef Jarosciak
|| >
||
|| Why use a string for DateDiff?
||
||
|| Public Function MinutesThisMonth() As Long
|| Return DateDiff( _
|| DateInterval.Minute, _
|| New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), _
|| DateTime.Now _
|| )
|| End Function
||
|| HTH,
|| Mythran
||
|
|
 
Jay B. Harlow said:
Doh!

I should add: any month end may cause a problem, as the first Now may
return
12/31 while the second may return 1/1...

Jay

| Mythran,
| Of course if you call your function at Midnight on Dec 31st you may get
| really interesting results.
|
| The first Now, may return 31 Dec 2005, while the second & third Now
return
1
| Jan 2006, which would cause an entire years worth of minutes to be added
| in...
|
| I find Armin's example of storing Now in a separate variable to be much
more
| reliable & less likely to cause obscure bugs...
|
| Just a thought
| Jay
|
| ||
|| || > Dim minthismonth As Integer = DateDiff(DateInterval.Minute,
|| > CType(("01/" & Date.Now.Month & "/" & Date.Now.Year),
System.DateTime),
|| > Now())
|| >
|| > is showing result of : 272895
|| >
|| > Which cannot be the truth. Today is 15th, so lets round it:
|| > 15(days) x 24(hours) x 60(minutes) = 21600 minutes
|| >
|| > Result shouldnt be 272895, but little less than 21600.
|| > Can someone see what is wrong with this code?
|| >
|| > Thanks
|| > Jozef Jarosciak
|| >
||
|| Why use a string for DateDiff?
||
||
|| Public Function MinutesThisMonth() As Long
|| Return DateDiff( _
|| DateInterval.Minute, _
|| New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), _
|| DateTime.Now _
|| )
|| End Function
||
|| HTH,
|| Mythran
||
|
|

If it is really that important, it should be noted that yes, you are
correct.

Public Function MinutesThisMonth() As Long
Dim d As DateTime = DateTime.Now
Return DateDiff(DateInterval.Minute, New DateTime(d.Year, d.Month, 1),
d)
End Function

:)

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top