I have problem when use DateTime class

I use DateTime.Now in my program and it work correctly.

I run my program 24hrs/day .

But it work correctly for 6hrs after that it retrun wrong date and time to my program

and I have to restart the program to make it work correctly again.

example

I run program at 31/10/2006 but it return 299/12/2006 to me.

I don't know what is the problem.

I can't fix it.

Anyone can help me

Thank




Answer this question

I have problem when use DateTime class

  • --Alan---

    ahmedilyas wrote:
    can you show us the code you are using

    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString("00");
    string filename = "log/TBG" + tempname + ".txt";
    string filename_temp = "temp/TBG" + tempname + ".txt";
    string filename_server = server_st + tempname + ".txt";
    //file handle for local log
    FileStream tbg_log = new FileStream(filename, FileMode.Append, FileAccess.Write);
    StreamWriter tbg_writer = new StreamWriter(tbg_log);
    //file handle for server temp log
    FileStream server_temp = new FileStream(filename_temp, FileMode.Append, FileAccess.Write);
    StreamWriter temp_writer = new StreamWriter(server_temp);

    lock (tbg_log)
    {
    //if connection from com1
    if (source.Port == st_port[0])
    {
    if (buf.Substring(0, 3) == "1NZ" || buf.Substring(0, 3) == "1ZZ" || buf.Substring(0, 3) == "3ZZ")
    {
    //write to temp file
    string data_value = buf + ",1," + DateTime.Now.Date.ToShortDateString() + "_" + DateTime.Now.ToLongTimeString();



  • Rod Yager

    can you show us the code you are using

  • Bianca J.

    Bizarre problem, to put it mildly. DateTime.Now does a fair amount of processing. First, it obtains the current UTC time with the GetSystemTimeAsFileTime() API function. It then adjusts that time by looking at the time zone and whether or not daylight savings time is in effect. That last one seems relevant, we just went through the change this past weekend in the USA.

    To see if this has anything to do with your problem, start using DateTime.UtcNow instead of DateTime.Now. Let us know if the problem goes away...



  • Nick...A

    And this is the result of my program

    TBG20061030

    TBG20061031

    PAS20061031

    TBG200612299<<<< WRONG DATE AND TIME AFTER RUN PROGRAM FOR 6HRS.

    PAS200612299 <<<< WRONG DATE AND TIME AFTER RUN PROGRAM FOR 6HRS.

    Anyone know about this problem

    Please help me!!!!

    Thank



  • I have problem when use DateTime class