CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2006
    Posts
    27

    Wink Another Date and Time question . But it's more difficulty!!!

    How can I insert the System time(current time) into access like the former
    in C++ (eg. 2006-04-19 16:41) ???
    The time in access is datetime type!!!
    Jeff Fan
    No change is the worst!!!
    The principal of mine
    msn:[email protected]
    If you like coding , add me.

  2. #2
    Join Date
    Apr 2006
    Posts
    27

    Unhappy Re: Another Date and Time question . But it's more difficulty!!!

    I know in Java , it can use the method getdate().
    But I have scanned many meterials , that I can't find in C++
    Jeff Fan
    No change is the worst!!!
    The principal of mine
    msn:[email protected]
    If you like coding , add me.

  3. #3
    Join Date
    Apr 2006
    Posts
    27

    Thumbs up Re: Another Date and Time question . But it's more difficulty!!!

    retcode=SQLExecDirect(hstmt,(UCHAR*)"insert into package(Protocol,SrcAddr,DstAddr,SrcPort,DstPort,TotalLen,GetTime)values('a','b','c','d','e',2000000,'3-2-2006')",SQL_NTS);//SQL语句执行出错


    the quote '3-2-2006' of the sentence , can i substitue with the system

    time???Like the first thread.
    Jeff Fan
    No change is the worst!!!
    The principal of mine
    msn:[email protected]
    If you like coding , add me.

  4. #4
    Join Date
    Jan 2005
    Posts
    4

    Re: Another Date and Time question . But it's more difficulty!!!

    you can try COLEDateTime.

    <pre>
    COleDateTime curTime= COleDateTime::GetCurrentTime( );
    CString str = curTime.Format(_T(" %Y-%m-%d %I:%M"));
    </pre>
    Cheers,
    Alok Gupta
    visit me at http://www.thatsalok.com

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Another Date and Time question . But it's more difficulty!!!

    • Using C standard library functions:
      Code:
         time_t timer = time(NULL);
         tm *loctime = localtime(&timer);
         char* pszDateTime = (char*)malloc(17);
      
         sprintf(pszDateTime, 
                 "%d-%02d-%02d %02d:%02d\0", 
                 loctime->tm_year + 1900,
                 loctime->tm_mon,
                 loctime->tm_mday,
                 loctime->tm_hour,
                 loctime->tm_min); 
      
         // ... you have now formatted datetime string
         
         free(pszDateTime);
    • Using MFC:
      Code:
         CTime locTime   = CTime::GetCurrentTime();
         CString strTime = locTime.Format(_T("%Y-%m=%d %H:%M"));
         // also can use COleDateTime instead of CTime
    • Using Windows API
      Code:
         SYSTEMTIME st;
         ::GetLocalTime(&st);
         char* pszDateTime = (char*)malloc(17);
      
         sprintf(pszDateTime, 
                 "%d-%02d-%02d %02d:%02d\0", 
                 st.wYear,
                 st.wMonth,
                 st.wDay,
                 st.wHour,
                 st.wMinute); 
         
         // ... you have now formatted date string
         
         free(pszDateTime);


    NOTES:
    1. You can further substitute datetime string in the SQL statement also by using sprintf or in case of using MFC by CString::Format.
      Example:
      Code:
         CString strSQL;
         strSQL.Format(_T("INSERT INTO Cats(Name, Owner, Birthdate)")
                       _T("VALUES('Kitty','John Smith','%s')"),
                       strDateTime);
    2. The above are just examples. You can also use sprintf/CTime::Format/CString::Format to direct format the SQL statement at once (without an intermediary datetime string).
      Example:
      Code:
         CTime locTime = CTime::GetCurrentTime();
         CString strSQL;
         strSQL.Format(_T("INSERT INTO Cats(Name, Owner, Birthdate)")
                       _T("VALUES('Kitty','John Smith','%04d-%02d-%02d')"),
                       locTime.GetYear(),
                       locTime.GetMonth(),
                       locTime.GetDay());
    3. The examples above give the local time and not the system time (UTC).
    4. Please do not create other threads with links to this one, simply to show that you have a problem here.
    Last edited by ovidiucucu; May 17th, 2006 at 08:06 AM. Reason: example added
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Apr 2006
    Posts
    27

    Thumbs up Re: Another Date and Time question . But it's more difficulty!!!

    I'm sorry that I said by mistake , the former should be 2006-04-19 16:41:56
    , it should append the secend.

    By the way , what's the head file in different enviroment? (eg. API and MFC)
    Jeff Fan
    No change is the worst!!!
    The principal of mine
    msn:[email protected]
    If you like coding , add me.

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Another Date and Time question . But it's more difficulty!!!

    Quote Originally Posted by [email protected]
    I'm sorry that I said by mistake , the former should be 2006-04-19 16:41:56
    , it should append the secend.
    So, what's the problem? Given examples above, choose one of methods, read documentation for sprintf, and/or CString, and/or CTime, and format the string as you wish.
    Quote Originally Posted by [email protected]
    By the way , what's the head file in different enviroment? (eg. API and MFC)
    If you have installed MS Visual C++, you can use AppWizard to create an MFC or Win32 Application. The wizard will automatically include the required headers (for example <afxwin.h> or <windows.h>).
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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