strtime issues

Im kinda stymied by the fact that _strtime is in the 24 hour format.. Can somebody point out how to convert this to 12 hour format?

1
2
3
4
5
6
7
8
9
10
11
12
/* Turbo c++ is my designated compiler */

#include <iostream.h>
#include <time.h>

int main (){
char timeStr [9];
_strtime(timeStr);
//conver to 12 hour format here
cout<<"Time is: "<<timeStr;
return 0;
} 
http://www.cplusplus.com/reference/ctime/strftime/ You should give it a format flag. Also, iostream.h and time.h are obsolete. You should be using iostream and ctime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>

int main()
{
    std::size_t const maxCharacters = 12;
    char timeStr[maxCharacters]; //HH:MM:SS *M
    //format will be either %r (c++11 enabled)
    //or %I:%M:%S %p (c++11 disabled)
    std::time_t timer = time(0);
    std::strftime(timeStr, maxCharacters, "%r", std::localtime(&timer));
    std::cout<<"Time is: "<<timeStr;
    return 0;
} 


I honestly would do a different approach but pretty tired.

*edit I took too long typing and yanson beat me :P
Last edited on
Topic archived. No new replies allowed.