CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2001
    Posts
    244

    GetDateFormat is one day off

    When I run the below function, the date gives me 1st DEC 2009 instead of 2nd DEC 2009

    I am in Sydney (+10 GMT), its 10:50am here right now. If I change the Windows time to 2:50pm, the date is now correct.

    So it looks like the below function not taking into account the +10GMT timezone adjustment?


    Code:
    CString CMyDlg::NLSDateFormat(CTime tm)
    {
    	SYSTEMTIME systime;
    	if (tm == 0) {
    		// get the current time
    		GetSystemTime(&systime);
    	}
    	else 
    	{
    		// convert the CTime
    		tm.GetAsSystemTime(systime);
    	}
    
    	int size;
    	CString cs;
    	size = ::GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime, NULL, NULL, 0);
    	::GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime, NULL, cs.GetBuffer(size), size);
    	cs.ReleaseBuffer();
    	return cs;
    }

    I call the function using NLSDateFormat(0);
    Last edited by Anarchi; December 1st, 2009 at 07:00 PM.

  2. #2

    Re: GetDateFormat is one day off

    Yep, you said it. GetSystemTime is in UTC. Use GetLocalTime instead, and it should be fixed.

  3. #3
    Join Date
    Nov 2001
    Posts
    244

    Re: GetDateFormat is one day off

    No worries I think I figured it out. I changed GetSystemTime to GetLocalTime.

    All these time functions are very confusing sometimes lol

    [edit] surfdabbler thanks anyway

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured