I was wondering if anyone could suggest a utility library that has useful functions for handling dates in ASP.NET easily taking away some of the leg work you normally have to do when handling dates?
Subsonic Sugar has some really nice functions:
http://subsonichelp.com/html/1413bafa-b5aa-99aa-0478-10875abe82ec.htm
http://subsonicproject.googlecode.com/svn/trunk/SubSonic/Sugar/
Is there anything better out there?
I was wanting to work out the start(mon) and end(sun) dates of the last 5 weeks.
I was thinking something like this:
DateTime Now = DateTime.Now;
while(Now.DayOfWeek != DayOfWeek.Monday)
{
Now.AddDays(-1);
}
for(int i=0; i<5;i++)
{
AddToDatesList(Now, Now.AddDays(7));
Now.AddDays(-7);
}
but this seems crappy? Plus this is not exactly what i want because i need the time of that start date to be 00:00:00 and the time of the end date to be 23:59:59
Is there a specific problem you are trying to handle with dates? If the existing date API in .NET can handle your problem cleanly, I see no reason to consider a 3rd party library to do it. When I was in .NET, we had to deal with dates quite a bit, and the standard libraries provided a fair amount of functionality to us.
What exactly do you want to do that System.DateTime and System.Timespan can't handle?
CSLA has a useful helper class called SmartDate that addresses quite a lot of the problems when using dates in real applications. As far as I can recall it's coupled to the rest of the framework.
Related
I had a hard time finding anything on this critical problem so I decide to post this to help users running into the same issue.
The Chrome embedded Flash Player (Pepper) has a bug concerning date timezoneOffset that leads to some unpredictable behaviors when your application deals with dates.
As we provide a commercial Flex calendar component (KC-Calendar), this issue is critical for our clients and for their applications.
The issue details can be found here: http://code.google.com/p/chromium/issues/detail?id=154060
(please vote)
Waiting for a fix, if someone has a workaround, it could be really helpful.
Thanks in advance!
We've dealt with such issues before, and honestly--rather than playing around with trying to serialize Dates--IMO the best solution (unless you WANT auto timezone conversion) is to simply pass date data types as strings. We do this cleanly using Java and ActionScript getters/setters which (correctly) set the serialized "date" objects in our DTO by parsing the un-adjusted date strings.
Ex. when you set a Date object's value, in the setter you convert Date to String & set a String field for that date. In the getter you get the String value for that date and parse it, returning a new Date object. Do this on server and client side.
The only minor caveat, obviously, is that you need to ensure the Flex & server side String-to-Date/Date-To-String functions use the same date format. Just put a nice comment explaining this (so nobody screws with it) next to your serialization date parsers/formatters and you're fine.
I expect this issue will probably go away once an HTML5-based Flex compiler is released (it's really just a Flash feature which isn't the desired behavior in our cases).
I've tried to do my homework on this...
I'm migrating a site from classic asp to asp.net 4.0
Before I rework all the code, I wanted to get clarification that the way i think my database code should be updated is fairly correct. (I'm using SQLClient objects)
First, when old code would use forward only recordsets or access single record/value, I'm fairly sure i'd use SqlCommand and SqlDataReader. That is pretty straightforward.
Now, when my old code navigates a recordset (movePrevious, find, etc.)... I believe the way to go is with SqlDataAdapter and DataTable to retrieve the data and DataView to filter/find/navigate the data.
I'n looking to do a quick migration... i don't want to get too bogged down in the new entity framework... so - just trying to stick with similar objects/logic.
I know this is one of those questions that doesn't really have a right/wrong answer... and I think that is part of my problem in finding an answer - there are so many.
Thanks,
bill g
I answered something similar here.
You only really need to know about the database bit here. For you it's a really good point to start as you mean to go on.
It terms of how deal with the data returned, it's up to you. I'd recommend custom class style dataset approach which will make life easier in the long run but of course in the short term, you'll have to generate them all which can be made easier by CodeSmith*. That in itself has an upfront cost but will, again, save time in the long run.
*Other code generation tools are available.
If your main issue is the database I propose to move and learn the Subsonic 2 (not subsonic 3, too slow) or any other DAL.
See here some examples:
http://subsonicproject.com/docs/Main_Page
Take some time to understand it, make some tests, see what really do, after wille you can't do with out it, but you need some time to understand the logic, and setup it to your project.
Get this project to play with and see some dal in action, ready to run.
I have seen countless forum posts complaining of issues in GWT when handling dates. I am still very unclear as to what is "wrong" with the date handling, what special considerations need to be made, and when/where/why methods like Date.setMinutes(int minutes) should not be used.
Does anybody have feedback?
Way back in the beginning of Java (i.e., Java 1.0), the date/time api largely (solely?) consisted of the Date class. The Java folks were aware that it lacked robustness, so they added the Calendar class in Java 1.1 and tried to change the Date class into a value object by deprecating most of it. Unfortunately, the Calendar class itself wasn't very well thought-out (see here) and we're stuck with what many consider to be a monstrosity.
Bringing us up to today, GWT supports Date because, well... how can you live without dates?, but doesn't support Calendar/GregorianCalendar/TimeZone because it's so ugly and surely there has to be a better answer. Sadly, no one's thought of it in over 3 years as Calendar support was requested way back in January of 2007 and marked as planned by April 2008.
In short, go ahead and use the the deprecated Date methods in your GWT code, if that will work for what you need to do.
EDIT: GWT's CalendarUtil class might also come in handy.
This problem makes me really sad. I'm working with a pretty archaic web application with an eclectic mix of programming languages. Previously, it stored any and all datetimes in a Datetime field in our MySQL database. Until now, everything has been done in server time, which happens to be US/Pacific. I was tasked with adding "Timezone functionality" to the application. I have been pretty successful with converting most of the application.
My only remaining problems are the use of ColdFusion specific datetime functions. There appears to be many lines of code that use some of these functions. My ideal situation would be to be able to adjust the timezone on my Application.cfm page and not need to worry about the timezone for any of these functions. I've done quite a bit of searching and I can't seem to find anything. Any help would be awesome! Please let me know if I can provide any more details.
see: How to handle timezones in CFML?
I'm building a new ASP.NET web application based on a legacy one (Classic ASP). The people, who build this code 4, 5 years ago, used a lot of VBScript functions like DateDiff.
I'm currently working on a simulator that does a lot of calculations with dates. And I'm getting a annoying difference between these dates because I don't have a easily manner to get the difference between months in c# like they did in VB 6.
So, I choose the path of using the DateAndTime.DateDiff from Microsoft.VisualBasic namespace on my ASP.NET web application.
You guys know if there are some implications on that? I'm a little concerned with this approach.
Cheers,
The only implication is that you're adding another assembly to your deployment, and using functionality that many C# developers (other people that might need to maintain your code) are not aware of. But personally I don't think there's much wrong with it. The MSDN Library documentation is typically good, and you can also add a few comments if you want to explain why your using it.
Edit: I also want to note that Microsoft.VisualBasic was implemented from scratch for .NET. It does not contain ancient code.
One possibility is to use the TimeSpan as Itay indicated, but divide by 30.4375 (the average days per month), ignoring fractional months. This is close enough for things I'm currently working on, but may not be accurate enough based on your needs. For instance, The difference between February 1 and March 1 would be 0, which (based on a 30.4375 day month definition) is correct; it may not be correct for your particular purposes, however.
It's good thing you are concerned with that, because you should not use any of of the classed in the VisualBasic namespace.
The .net library offer a much better solution called TimeSpan, use it in the following way: (dt1, and dt2 are DateTimes)
TimeSpan ts = dt2 - dt1; //Or ts = dt1.Subtract(dt2)
As far as i would like to help, I really think a quick glance at the intellisence will tell you the rest (just write this in Visual Studio, and add ts.)
EDIT for real month counting: (y2 - y1) * 12 + m2 - m1, what the big deal?