1 답변
-
플랫폼에 관계없이 동작하는 코드는 다음과 같습니다.
#include <iostream> #include <string> #include <stdio.h> #include <time.h> // 현재시간을 string type으로 return하는 함수 const std::string currentDateTime() { time_t now = time(0); //현재 시간을 time_t 타입으로 저장 struct tm tstruct; char buf[80]; tstruct = *localtime(&now); strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct); // YYYY-MM-DD.HH:mm:ss 형태의 스트링 return buf; } int main() { std::cout << "지금 시간은:" << currentDateTime() << std::endl; }
결과 :
지금 시간은:2016-01-15.18:44:25
datetime format에 관해서는 여기를 참고하세요
댓글 입력