Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

In an application I am writing I need to get the Date & Time from the PC do a little conversion and send it out to a board. I have sat down written the routine
which works. Sent it out to the end user customer got a JPEG back of it reading the date in correctly, below is some demo code of what I am trying to do which works on my PC
C#
strDateTime = Convert.ToString(DateTime.Now);
strDateTime = strDateTime.Trim();
MessageBox.Show(strDateTime);
MessageBox.Show(strDateTime.Length.ToString());

//trap if not 19 recall function
if (strDateTime.Length != 19)
{
    //btnUsePCTime_Click(null, null);
    button9_Click(null, null);
}

Date = strDateTime.Substring(0, 10);
Date = Date.Trim();
txtPCTime.Text = Date;
Time = strDateTime.Substring(11, 5);
Time = Time.Trim();
MessageBox.Show(strDateTime + "\n" + Date + "\n" + Time);
It returns in the text box txtPCTime the date, and txtPCTime the Date (I know it's on the change list!). However the customer gets in his date box (txtPCTime ?)
17/01/13 1 and in the time box (txtPCDate ?) :58:1 this could be due to a non standard DateTime.Now value returning 13 and not 2013 if this was the case why doesn't the if() catch it ?
Glenn
Posted

Glen, why are you doing all that?
The problem is almost certainly that the customer PC is configured to show dates differently form you development machine. Since you are using Convert.ToString it will use the current machine settings to configure the string it returns.

Use specific formats if that is what you want:
C#
DateTime now = DateTime.Now;
string date = now.ToString("dd/MM/yyyy");
string time = now.ToString("HH:mm:ss");

There is a list of all the formatting codes here: Formatting a DateTime for display - format string description[^]
 
Share this answer
 
Comments
glennPattonWork3 17-Jan-13 9:19am    
Thanks for that,
OriginalGriff 17-Jan-13 9:25am    
You're welcome!
HI,

Here is a nice link that will be helpful in future for converting the datetime formats.
check out this.
String Format for DateTime [C#]

Thanks
 
Share this answer
 
Comments
glennPattonWork3 17-Jan-13 9:30am    
Book marked it, worse thing is I have seen this before!
[no name] 17-Jan-13 9:38am    
Its alright dear... The main thing is to learn everyday... Mistakes makes a person perfact.
you can convert DateTime values to string to whichever way you find necessary, such as:

C#
DateTime date = DateTime.Now.ToString(dd-MM-yyyy); // This will convert to day-month-year


And in your if() statement you are checking if the length of the string is not 19. This check actually doesn't make nay sense. Tell me what exactly you want to do and maybe I can suggest a better if() statement.
 
Share this answer
 
Comments
glennPattonWork3 17-Jan-13 9:50am    
I think I have it sorted ta muchly!

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