Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
3.18/5 (4 votes)
See more:
Hello Experts
Belated Merry Christmas and advanced happy new year

I have an critical issue with Datetime in C# client server application

In my Client server application, i have my server (database) in one system and client applications installed in many other systems

I have tough time in converting string to datetime in these systems which have different date time formats

Say, in server system the datetime format is dd/MM/yyyy hh:mm:ss tt
in Client 1 the datetime format is MM/dd/yyyy hh:mm:ss tt
in Client 2 the datetime format is yyyy/MM/dd hh:mm:ss tt
in Client 3 the datetime format is dd-MM-yyyy hh:mm:ss tt

How do i convert string to datetime in this tricky situation, i cant ask my client to change to datetime format in each system to a unique datetime format :(

I have googled a lot on these but nothing seems to help me out

Please help me experts :(

Let me know if you need more clarity on this post.
Posted
Comments
Ron Beyer 27-Dec-13 1:05am    
Did you write the clients? Why use strings? Datetimes are better represented as numerical values like doubles or longs, store them in the lowest level format and forget about strings...
Sergey Alexandrovich Kryukov 27-Dec-13 2:37am    
Agree. For .NET, it should be System.DateTime, for SQL databases — DATE. Forget about strings.
—SA

That's why storing DateTimes as string in database is a bad practice and should be avoided at all costs.

The best to do is :
- to store values as DateTime in the database, preferably in UTC ;
- to display values on clients according to local time/regional format. Below are some useful links about methods you can use for that.

DateTime.ToLocalTime Method[^]
DateTime.ToUniversalTime Method[^]
DateTime.ToString Method (IFormatProvider)[^]
Coordinated Universal Time[^]

Examples :

Storing a DateTime value in database from client:
C#
DateTime date = dateTimePicker.Value.ToUniversalTime();
cmd.CommandText = "UPDATE Invoices SET InvoiceDate = @myDate WHERE InvoiceId = @currentId";
cmd.Parameters.AddWithValue("@myDate", date);
cmd.Parameters.AddWithValue("@currentId", currentId);
cmd.ExecuteNonQuery();


Retrieving and displaying a DateTime value on client application:
C#
cmd.CommandText = "SELECT InvoiceId, InvoiceDate FROM Invoices";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
   DateTime currentDate = ((DateTime)dr["InvoiceDate"]).ToLocalTime();
   invoiceDateTextBox.Text = currentDate.ToString(CultureInfo.CurrentUICulture);
}


This way you won't have to bother anymore about DateTime storage and display formats.

These are just some skeletons and give a basic idea about DateTime handling; I hope you get the big picture and are able to adapt it to your current code.
 
Share this answer
 
Comments
Member 10385392 27-Dec-13 3:15am    
Thank you so much for the valuable solution phil. I will try this right away
phil.o 27-Dec-13 4:16am    
You're welcome :) Please don't forget to vote and mark the question as solved, if my answer has been any useful to you.
Member 10385392 27-Dec-13 4:18am    
Sure Phil :)
Hello

Please use following
C#
string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
string format = "dd/MM/yyyy hh:mm:ss tt";

DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);


UPDATES:

C#
You can set any format
string dateString1 = "Mon 16 Jun 8:30 AM 2008";
string dateString2 = "16 Jun 2008 8:30 AM";
string dateString3 = "2008 Jun 16 8:30 AM";
string format = "dd/MM/yyyy hh:mm:ss tt";

DateTime dateTime = DateTime.ParseExact(dateString1, format, CultureInfo.InvariantCulture);
DateTime dateTime = DateTime.ParseExact(dateString2, format, CultureInfo.InvariantCulture);
DateTime dateTime = DateTime.ParseExact(dateString3, format, CultureInfo.InvariantCulture);


Output will be for all three date
C#
16/Jun/2008 08:30:45 AM


Thanks,
Imdadhusen
 
Share this answer
 
v2
Comments
Member 10385392 27-Dec-13 2:17am    
thanks for the reply ... the string "dateString" can be in any format. How to go about this?
Member 10385392 27-Dec-13 4:40am    
Thank you for the valuable reply. I will try this

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