Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have two MySql tables. The link to first table from next table is Date and time as string.

C#
//Compute date and time to store in database
string Datetime = DateTime.Now.Date.ToString("yyyyMMdd") +
                         DateTime.Now.Hour.ToString() +
                         DateTime.Now.Minute.ToString() +
                         DateTime.Now.Second.ToString();

While the button clicked the datetime is computed and stored as string.

The problem is sometimes
the datetime stored as
first table  - 2010-12-14 12:09:31 &
second table - 2010-12-14 12:9:31

ZERO before 9 is absent in the second table.

While comparing tables date and time it never be true.
I don't know why this problem happening.
Please help me to solve this.

Thanks in Advance,
Saranya1388

[edit]code blocks added, "Ignore HTML" option disabled - OriginalGriff[/edit]
Posted
Updated 3-Jan-11 23:10pm
v2

Don't do it.
Store dates in the DB as DateTime, and use SQlCommand.Parameters.AddWithValue to add the DateTime.Now directly, rather than converting it to a string in the first place.

Additionally:
Don't do the convert like that anyway!
1) When dealing with DateTime.Now, get a single instance and work from that. Each time you get DateTime.Now, you get the current date and time - this can lead to odd bugs near midnight and especially near Jan 1st!
DateTime now = DateTime.Now;
string s = now.ToString("yyyyMMdd");
...

2) Don't do the conversion in multiple lines. If you use a single DateTime.ToString it will be more efficient, and work better as you will have better control over the string produced. There is a full list of ToString formats here: Formatting a DateTime for display - format string description[^] which should help.

If you change as suggested, your problem will disappear.
 
Share this answer
 
Comments
Bardy85 4-Jan-11 5:15am    
Hi OriginalGriff,

I agree with your first part about rather using DateTime in Database, but I don't agree with your second suggestion. What odd bugs do you get around midnight or around Jan 1st?

Regards
jerrykid 4-Jan-11 5:19am    
I agree with you, OriginalGriff,
And there is no need to create two line to store variable now & s
OriginalGriff 4-Jan-11 5:22am    
Bardy85: Think about it. If you do DateTime.Now and use the date part, then DateTime.Now and use the time part, what happens if the two reads are in different days? :laugh:
Can create data bugs that very hard to work out!
Bardy85 4-Jan-11 5:28am    
OriginalGriff: I see what you are worried about.
That nearly impossible though, cause that statement is excuted within a few nano seconds.
I think jerryKid conversion would be his best bet, if it's already in that format.
OriginalGriff 4-Jan-11 5:31am    
Bardy85: "That nearly impossible" :laugh: that's why you should code to avoid it - it's a real pain to track down what happened when all you have is what looks like corrupt data. Debugging an intermittent fault like that can take forever!
please try:
string Datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
 
Share this answer
 
Rather convert the date as follow.

string strDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");


Regards
 
Share this answer
 
Comments
Bardy85 4-Jan-11 5:18am    
Then I would suggest you use JerryKid example ToString("yyyy-MM-dd HH:mm:ss"), as this will always be formatted the same.

Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900