String Date @#$!@

firas489

Member
Joined
Apr 25, 2008
Messages
14
Programming Experience
5-10
Hi All,

Iam not an expert with handeling dates,
and all my programming projects where either server-side (where date is not an issue because programs are centralized) and client-side where the project didnt depend on the date a lot.

But now its different with me,
Im working on a project that its core depends on the date, and unfortunatly its client-side,
and its killing me to have to think of how to handle the date when the system date format changes...
And thats a problem, because the date the i try to handle is string,
meaning, if the system date format wasnt close (if not exactly) the same as the date string format, then i get thrown by limitless of errors and exceptions.
and of course, thanks to Microsoft, they didnt make a good DateValue or Date.Parse Functions...

let me give you an example of my frustration:
if the system date format is YY-MMM-DD
and i try to get the date value of a string that is formated the same as the system date format, i will get an error,
i did some testings, and found that, even the system date format is DD-MMM-YY, but DateValue will only read it as DD-MMM-YY
while in other situations, DateValue will only read strings if they match the system date format.

I cant find anything useful online that will help me at least understant how the date work on Windows,
and help me control it instead of it controling me...

BTW, i had worked on a function that will convert a date string to a certain format to be used else where (similar to the Format Function, but the date type can be string)...The reason i did this because i discovered that DateValue will be able to handle date in this format YYYY-MM-DD, even if the system date format is different...
But implementing it in programs seemed to be painful...


Bottom line, does anybody have any advice on how to handle dates while system date format is very much likely changable, and the date im handeling is string type????

Anything is appreciated,

Thanks in advance,


BTW, if im not the only one frustrated because of the date issue, then i will be more than happy to join a small team to create the ultimate date function that will give you the exact results regardless the system date format and regardless of the date type you are trying to handle...


Best Regards


Firas S Assaad
 
See if this will help you at all

VB.NET:
MessageBox.Show(Now.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.UniversalSortableDateTimePattern))
 
Bottom line, does anybody have any advice on how to handle dates while system date format is very much likely changable, and the date im handeling is string type????

Yeah.. dude, you really need to get to grips with the fact that:

Dim s as String = "01-MAR-1999"

IS NOT A DATE. It is a string that a human might read as a date.

Microsoft invented the DateTime type for a REASON. USE IT.


Then you can store all your dates as dates, and do maths on them like:

(oneDate - otherDate).TotalDays 'get the days between the dates

and you can format them:

myDate.ToString("yyyyMMdd HHmmss")

and you can parse them:

myDate = DateTime.ParseExact(myString, "dd-MMM-yy HH:mm:ss", CultureInfo.CurrentCulture)


DO NOT store your dates as strings. DO NOT parse to and from a string every time! Store as a date and if you happen to need a string, format it to a string at that moment (such as writing to a file)
 
Edit: i'm not 100% sure that ParseExact is available in .net 1.1 and msdn is being very slow for me to check..

Which is another good reason why not to store dates as strings! ;)
 
DO NOT store your dates as strings. DO NOT parse to and from a string every time! Store as a date and if you happen to need a string, format it to a string at that moment (such as writing to a file)

You are right cjard, and i personally never worked with dates as string in my life.
But the problem with this situation is that that data is not mine, im just developing a program that will do certain jobs and in the same time depends on the database for information.
And everything in the database is string, even digits are stored as string.
So that problem is by design.
And its impossible for me to change the database structure!

And i was wishing someone would have encountered the same problem and found a simple solution....


Thank you very much,



Best Regards
Firas Assaad
 
Option

Maybe you should let the folks who created the problem solve it for you. Have whoever maintains these date strings provide you with stored procedures that do the conversion into properly formatted dates. Then query using the SP's instead of inline SQL.

- Kane
 
Maybe you should let the folks who created the problem solve it for you. Have whoever maintains these date strings provide you with stored procedures that do the conversion into properly formatted dates. Then query using the SP's instead of inline SQL.

- Kane

Thanks for your reply Kane,

Unfortunately, having them fix the problem is not that simple.
Trust me, thats not even an option.

The other problem with this is that the data is stored in Access Database,
not a SQL Server or some advance database engines!!
And the worse part is that its stored in Access 97 file format!

And im not sure if its worth writting my own SP to do the job!


But anyhow, i appreciate your reply.

And if you have any other thoughts, i will be glad to hear them





Best Regards
Firas Assaad
 
Last edited:
Hello all,

first, thanks to every member who replied and helped in this thread.

I finally managed to create a work around...

If you are interested in knowing more about it, please visit the following link to my blog, thanks

my blog


Best Regards

Firas S Assaad
 
Back
Top