Click here to Skip to main content
12,945,697 members (54,035 online)
Rate this:
Please Sign up or sign in to 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
            strDateTime = Convert.ToString(DateTime.Now);
            strDateTime = strDateTime.Trim();
            //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 ?
Posted 17-Jan-13 2:59am
Rate this: bad
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[^]
glennPattonWork 17-Jan-13 9:19am
Thanks for that,
OriginalGriff 17-Jan-13 9:25am
You're welcome!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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

glennPattonWork 17-Jan-13 9:30am
Book marked it, worse thing is I have seen this before!
Rate this: bad
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.
glennPattonWork 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
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,170
Maciej Los 2,443
Jochen Arndt 1,910
ppolymorphe 1,775

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2017
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