2014-06-11 15 views
7

Sto provando a stampare una data da una stringa come "01/01/01" e ottenere qualcosa come "Lunedì 1 gennaio 2001.Come si stampa una data in C?

Ho trovato qualcosa con l'uomo di ctime ma davvero non capisco come usarlo.

Qualsiasi aiuto?

Grazie,

+0

Condividi ciò che si trovato per favore –

+0

In realtà, praticamente niente di più di un uomo. Ho appena capito che c'è una struttura con quella che possiamo riempire .. Ma non so come. Struct è "struct tm timep" – Difender

+0

http://www.java2s.com/Code/C/Development/PrintlocalandUTCtime.htm Vedere questo link può essere d'aiuto. –

risposta

5

È possibile utilizzare strptime per convertire la data stringa struct tm

struct tm tm; 
strptime("01/26/12", "%m/%d/%y", &tm); 

e quindi stampare struct tm nel formato data appropriata con strftime

char str_date[256]; 
strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm); 
printf("%s\n", str_date); 
+0

Funziona davvero bene, grazie mille! – Difender

4

strftime() fa il lavoro.

char buffer[256] = ""; 
{ 
    struct tm t = <intialiser here>; 
    strftime(buffer, 256, "%H/%M/%S", &t); 
} 
printf("%s\n", buffer); 
+0

La firma di 'strftime' è' size_t strftime (char * s, size_t max, const char * format, const struct tm * tm); '. Ti manca il parametro 'size_t'. – HAL

+0

Grazie, risolto. @HAL – alk

1

Probabilmente stai cercando strftime