Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
            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 17-Jan-13 2:59am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
            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[^]
  Permalink  
Comments
glennPattonWork at 17-Jan-13 9:19am
   
Thanks for that,
OriginalGriff at 17-Jan-13 9:25am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
glennPattonWork at 17-Jan-13 9:30am
   
Book marked it, worse thing is I have seen this before!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

you can convert DateTime values to string to whichever way you find necessary, such as:
 
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.
  Permalink  
Comments
glennPattonWork at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 353
1 OriginalGriff 304
2 George Jonsson 258
3 Shemeemsha RA 138
4 Animesh Datta 130
0 OriginalGriff 5,985
1 Sergey Alexandrovich Kryukov 5,341
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100