Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Date.subtract function


  • Please log in to reply
11 replies to this topic

#1 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 24 July 2012 - 12:38 PM

Hi Guys, in VB.net, when I need to find the time difference between 2 dates, all I need is to use the function DateDiff. But in NetMF, I can only use date.subtract method and it returns a Timespan object. The problem here is that, in a TimeSpan object, how can I get the total number of minutes between 2 dates? In the NetMF, the totalMinutes method is removed. Only left with the Minutes property which does not serve my purpose. So I have to fall back to use the following statements: if (Date.Now.substract(m_dteLastGPSInfo).tick/TimeSpan.TicksPerMinute )> 30 then .... Any good idea on how to deal with this? Thanks in advance.

#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 24 July 2012 - 01:13 PM

Hi! If you're using framework 4.2 you can extend the Timespan class with totalminutes etc that you implement yourself by years x minutes_per_year + months x minutes_per_month...etc. Would this do you any good?

#3 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 24 July 2012 - 01:13 PM

What is the issue with your work around?

Seems like it would do what you want, if you use the code a lot you may benefit from wrapping it as a static method

//C#
public static int MinutesDifference(DateTime lastGPSInfo){
 return (DateTime.Now.Subtract(lastGPSInfo).Ticks/TimeSpan.TicksPerMinute);
}

//VB.Net - My VB is very rusty example is from memory so may not work at all ;-)
Public Shared Function MinutesDifference(ByVal lastGPSInfo As DateTime) As Int
 Return (DateTime.Now.Subtract(lastGPSInfo).Ticks/TimeSpan.TicksPerMinute)
End Function

Nak.

#4 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 24 July 2012 - 01:26 PM

What is the issue with your work around?

Seems like it would do what you want, if you use the code a lot you may benefit from wrapping it as a static method

//C#
public static int MinutesDifference(DateTime lastGPSInfo){
 return (DateTime.Now.Subtract(lastGPSInfo).Ticks/TimeSpan.TicksPerMinute);
}

//VB.Net - My VB is very rusty example is from memory so may not work at all ;-)
Public Shared Function MinutesDifference(ByVal lastGPSInfo As DateTime) As Int
 Return (DateTime.Now.Subtract(lastGPSInfo).Ticks/TimeSpan.TicksPerMinute)
End Function

Nak.


Hi Guys,

There is nothing wrong with my workaround, except that it needs more code to do as compare to DateDiff method. As you know code size for our little Netduino is precious. :)

#5 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 24 July 2012 - 01:38 PM

There is nothing wrong with my workaround, except that it needs more code to do as compare to DateDiff method. As you know code size for our little Netduino is precious. :)

The memory overhead of your work-around has virtually no impact on memory usage as long as you define it in one place only as suggested by nakchak or implement it as an extension method of the Timespan class.

Personally, I find the latter to be slightly more elegant ;-)

#6 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 24 July 2012 - 01:58 PM

Extension methods are very nice way of doing it, it more out of laziness that I didn't write the method as one (plus until you get your head around how they work they deff confuse most people who encounter them the first time ;) ) Does any one know if you can you do extension methods in VB, or is it one of the minor differences between the syntaxes? (like how c# didn't used to be able to have default values on method arguments). Nak.

#7 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 24 July 2012 - 02:06 PM

"extension methods", that's the "using" statement in the middle of a class, right?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#8 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 24 July 2012 - 02:55 PM

Ok so the correct syntax for the above would be like this in C#
public static int Totalminutes(this Timespan ts)
{
      return 3577444;
}


#9 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 24 July 2012 - 03:13 PM

Rather outside a class, in C# you would write something like this:

public int Totalminutes(Timespan this)
{
      return 3577444;
}
I'm pretty sure you can do this in VB too but I don't know the syntax.

I honestly have no idea what you mean. If I add that code to an empty project it will give errors. 'this' is a keyword, is that intended? Am I on the border of learning something new here? :D
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#10 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 24 July 2012 - 03:21 PM

Sorry, that was from the top of my head (I'm in the swedish archipelago and have only my iPhone) and so the syntax is slightly different. There was another thread about extension methods a while ago where you reacted exactly the same, I can't find it right now, maybe you can or nakchak knows the correct syntax? EDIT: Also note that extension methods require fw 4.2.

#11 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 24 July 2012 - 04:01 PM

fyi http://msdn.microsof...y/bb383977.aspx basically its a static method with syntax sugar that allows you to pass the value type as the first (implied) argument. its how the all the ASP.Net MVC HTML helpers work. Nak.

#12 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 24 July 2012 - 04:11 PM

Pressed the wrong button and managed to edit my own post above. Anyway the syntax svould be correct now I hope.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.