473,782 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DateDiff for C# - SR

sr
Anyone know of a better way to simulate a datediff for C#, i.e.,
without referencing the VB.NET runtime?

Only added the functionality that was needed for me so it is not
the full implementation of the DateDiff in the VB.NET runtime.

private int DateDiff( string Interval, DateTime Date1, DateTime Date2 )
{
int intDateDiff = 0;
TimeSpan time = Date1 - Date2;
int timeHours = Math.Abs( time.Hours );
int timeDays = Math.Abs( time.Days );

switch(Interval .ToLower())
{
case "h": // hours
intDateDiff = timeHours;
break;
case "d": // days
intDateDiff = timeDays;
break;
case "w": // weeks
intDateDiff = timeDays / 7;
break;
case "bw": // bi-weekly
intDateDiff = (timeDays / 7) / 2;
break;
case "m": // monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = timeDays / 30;
break;
case "bm": // bi-monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 30) / 2;
break;
case "q": // quarterly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 90);
break;
case "y": // yearly
intDateDiff = (timeDays / 365);
break;
}

return intDateDiff;
}

SR

Nov 16 '05 #1
5 7240
I'm not sure why you'd want a function "just like datediff" for c#.. and if
you do, why not reference the vb.net namespace?

(it's not the vb.net *runtime*... vb.net and c# use exactly the same
runtime. the vb.net namespace / assembly is just another batch of .net
code)

--
-Philip Rieck
http://philiprieck.com/blog/

-
"sr" <30**@bellsouth .net> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Anyone know of a better way to simulate a datediff for C#, i.e.,
without referencing the VB.NET runtime?

Only added the functionality that was needed for me so it is not
the full implementation of the DateDiff in the VB.NET runtime.

private int DateDiff( string Interval, DateTime Date1, DateTime Date2 )
{
int intDateDiff = 0;
TimeSpan time = Date1 - Date2;
int timeHours = Math.Abs( time.Hours );
int timeDays = Math.Abs( time.Days );

switch(Interval .ToLower())
{
case "h": // hours
intDateDiff = timeHours;
break;
case "d": // days
intDateDiff = timeDays;
break;
case "w": // weeks
intDateDiff = timeDays / 7;
break;
case "bw": // bi-weekly
intDateDiff = (timeDays / 7) / 2;
break;
case "m": // monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = timeDays / 30;
break;
case "bm": // bi-monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 30) / 2;
break;
case "q": // quarterly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 90);
break;
case "y": // yearly
intDateDiff = (timeDays / 365);
break;
}

return intDateDiff;
}

SR

Nov 16 '05 #2
Sr,
Rather then attempt to reinvent the wheel; why not simply reference the
Microsoft.Visua lBasic assembly, then you can use the
Microsoft.Visua lBasic.DateAndT ime.DateDiff function?

using Microsoft.Visua lBasic;
private int DateDiff( DateInterval interval, DateTime date1, DateTime
date2 )
{ return DateAndTime.Dat eDiff(interval, date1, date2,
FirstDayOfWeek. Sunday, FirstWeekOfYear .Jan1) }
The Microsoft.Visua lBasic is installed with the Framework itself, so it's
"guaranteed " on being available...

If you choose not to use Microsoft.Visua lBasic: I would simply use the
various properties of TimeSpan rather then define the timeHours & timeDays
variables, and define an Enum for the interval.

Something like:

enum DateInterval
{
Hours
Days
} TimeSpan time; if (Date1 < Date2)
time = Date2 - Date1;
else
time = Date1 - Date2;
switch(interval )
case DateInterval.Ho urs return time.TotalHours ; break;
case DateInterval.Da ys return time.TotalDays; break; ....
default:
throw new ArgumentOutOfRa nge( "interval", interval, "Invalid
interval value")
}

Hope this helps
Jay

"sr" <30**@bellsouth .net> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. . Anyone know of a better way to simulate a datediff for C#, i.e.,
without referencing the VB.NET runtime?

Only added the functionality that was needed for me so it is not
the full implementation of the DateDiff in the VB.NET runtime.

private int DateDiff( string Interval, DateTime Date1, DateTime Date2 )
{
int intDateDiff = 0;
TimeSpan time = Date1 - Date2;
int timeHours = Math.Abs( time.Hours );
int timeDays = Math.Abs( time.Days );

switch(Interval .ToLower())
{
case "h": // hours
intDateDiff = timeHours;
break;
case "d": // days
intDateDiff = timeDays;
break;
case "w": // weeks
intDateDiff = timeDays / 7;
break;
case "bw": // bi-weekly
intDateDiff = (timeDays / 7) / 2;
break;
case "m": // monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = timeDays / 30;
break;
case "bm": // bi-monthly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 30) / 2;
break;
case "q": // quarterly
timeDays = timeDays - ((timeDays / 365) * 5);
intDateDiff = (timeDays / 90);
break;
case "y": // yearly
intDateDiff = (timeDays / 365);
break;
}

return intDateDiff;
}

SR

Nov 16 '05 #3
I commend your rare common sense Jay !

I've always thought that if an assembly has something you find useful,
then why in the world would you want to rewrite it just because of the
assembly's name?

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #4
sr
I hear ya when someone besides MSFT writes a component and you
reference the component... that is what a component language is about.
But I don't feel .net should go down the path of referencing another
language runtime for every class that has common business functions
that any language should have innately available. Put those common
helper classes and enums into the system namespace exactly where the
Math class is! (all .net languages should have them available in the
system namespace - shouldn't have to reference another language).
Think how pissed VB coders would be if MSFT removed the DateAndTime
class from the vb.net runtime, but put it into the C# language only.
Then every VB.NET program would have to reference the C# runtime and
namespace in order to use it. Microsoft needs to create new FCL
Financial and DateAndTime classes (using decimal instead of double
where when dealing with currency) that are part of the core of the .net
framework. No one wants to write all that crap over and over again.
Sucks that I got to convert everything from decimal to double then back
to decimal. Would love to get rid of all my VB wrapper methods!

Nov 16 '05 #5
sr,
Financial ... classes (using decimal instead of double
where when dealing with currency) I never really understood why the (.NET) Financial class uses double instead
of Decimal either, I would think Decimal would be the far more logical
course for Financial objects! Only reason I can see them using Double is
historic reasons...

I objectized (encapsulated into classes) the Financial functions someplace,
I should dust them off & put them on my blog...
Put those common
helper classes and enums into the system namespace exactly where the
Math class is! (all .net languages should have them available in the
system namespace - shouldn't have to reference another language). Should they be in System? I would leave them in the Microsoft namespace and
consider removing them from the VisualBasic namespace, however I don't think
they need to be in the System namespace. Similar to how there are functions
in the Microsoft.Win32 namespace. And no they should not be in the Win32
namespace either, unless they deal specifically with Win32! I'm thinking
more simply Microsoft.DateA ndTime & Microsoft.Finan cial.
I hear ya when someone besides MSFT writes a component and you
reference the component... Microsoft should be able to (and should!) create components just like any
other company! I hope you are not suggesting they cannot nor should not!

Just a thought
Jay

"sr" <30**@bellsouth .net> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...I hear ya when someone besides MSFT writes a component and you
reference the component... that is what a component language is about.
But I don't feel .net should go down the path of referencing another
language runtime for every class that has common business functions
that any language should have innately available. Put those common
helper classes and enums into the system namespace exactly where the
Math class is! (all .net languages should have them available in the
system namespace - shouldn't have to reference another language).
Think how pissed VB coders would be if MSFT removed the DateAndTime
class from the vb.net runtime, but put it into the C# language only.
Then every VB.NET program would have to reference the C# runtime and
namespace in order to use it. Microsoft needs to create new FCL
Financial and DateAndTime classes (using decimal instead of double
where when dealing with currency) that are part of the core of the .net
framework. No one wants to write all that crap over and over again.
Sucks that I got to convert everything from decimal to double then back
to decimal. Would love to get rid of all my VB wrapper methods!

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4105
by: CJM | last post by:
I have an ASP page that lists files and folders in a directory. I'm using a cookie to record the last time this page was visited, and I intend to show links that are created/modified from that date minus 30mins to the present... eg effectively new and modified files: If DateDiff("n", subfolder.DateLastModified, sLastVisit ) < 30 Then.... etc On my test machine this mechanism works OK, but on the live server (Win2k, IIS5) I get some...
8
5930
by: inamori | last post by:
I face that problems 07/01/2003 06/30/2006 ---------> it should be 3 01/01/2003 02/28/2005 --------->could i get 2 years and 2 months 01/01/2003 03/01/2005 --------->could i get 2 years and 2 months and 1
6
16517
by: Lofty | last post by:
Hi all. I have to write an app that interacts with mySQL (I really must have done some evil, evil stuff in a previous life to be landed with this!) I need to work out the difference in days between values in the database and the current date. "No problem," thought I , "I'll just use the SQL DATEDIFF command." Heh! Well, the user interface I'm using didn't even recognise DATEDIFF as being a function, so I decided to visit the mySQL...
1
8008
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days until completion: DateDiff("d",,) is TODAY the appropriate syntax for today's date? Could the function/expression even be set up that way, or do I need to do something else to get 'days until completion'? (My Access manuals do not mention a TODAY...
4
11917
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS STARTINGDATE1 ENDINGDATE1 STARTINGDATE2
1
4984
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a blank. I've tried; > >DateDiff("y",-4,DateIn) and get errors > >Please any assistance would be greatly appreciated. >
7
15505
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using DateDiff in VB.NET which is only available in C# if you don't mind linking in Microsoft.VisualBasic.dll to your C# application. I wanted to avoid this so set about a pure C# solution which uses a combination of TimeSpan in whole days and the...
6
7650
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
2
2779
by: muddasirmunir | last post by:
i am using vb 6 , i had place two datepicker in form now i want to calcuate differcen of month in two date for this i used the function datediff i had try it withh many syntax but getting error like datediff(mm,date1,date2) datediff(mmm,date1,date2) datediff(month,date1,date2) datediff("mm",date1,date2) datediff("mmm",date1,date2) datediff("month",date1,date2)
0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.