DateDiff function in C# to simulate VBScripts by Robert Chartier


Contributed by
Rating: 2 stars2 stars2 stars2 stars2 stars / 18
January 31, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
This method is used to simulate the VBScript DateDiff Function. It is in Beta right now.

I have not yet implemented all of the parameters to the method, just the more commone ones

If you find any problems please email me with a description of the issue.


0.
1. /// <summary>
2. /// same common params as the VBScript DateDiff: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatediff.asp
3. /// /*Sample Code:
4. /// * System.DateTime dt1 = new System.DateTime(1974,12,16);
5. /// * System.DateTime dt2 = new System.DateTime(1973,12,16);
6. /// * Page.Response.Write(Convert.ToString(DateDiff("t", dt1, dt2)));
7. /// * */
8. /// </summary>
9. /// <param name="howtocompare"></param>
10. /// <param name="startDate"></param>
11. /// <param name="endDate"></param>
12. /// <returns></returns>
13. private double DateDiff(string howtocompare, System.DateTime startDate, System.DateTime endDate) {
14. double diff=0;
15. try {
16. System.TimeSpan TS = new System.TimeSpan(startDate.Ticks-endDate.Ticks);
17. #region converstion options
18. switch (howtocompare.ToLower()) {
19. case "m":
20. diff = Convert.ToDouble(TS.TotalMinutes);
21. break;
22. case "s":
23. diff = Convert.ToDouble(TS.TotalSeconds);
24. break;
25. case "t":
26. diff = Convert.ToDouble(TS.Ticks);
27. break;
28. case "mm":
29. diff = Convert.ToDouble(TS.TotalMilliseconds);
30. break;
31. case "yyyy":
32. diff = Convert.ToDouble(TS.TotalDays/365);
33. break;
34. case "q":
35. diff = Convert.ToDouble((TS.TotalDays/365)/4);
36. break;
37. default:
38. //d
39. diff = Convert.ToDouble(TS.TotalDays);
40. break;
41. }
42. #endregion
43. } catch(Exception e) {
44. diff = -1;
45. }
46. return diff;
47. }
48.



References:
VBScript DateDiff Function


Copy Source:
blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials