Last active
July 31, 2019 21:57
-
-
Save parzibyte/37c1ddfc6a7c70028b6675d7717f5914 to your computer and use it in GitHub Desktop.
Fecha y hora actual en ANSI C - https://parzibyte.me/blog/2019/07/31/c-obtener-formatear-fecha-actual/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Obtener y formatear fecha actual | |
en C | |
https://parzibyte.me/blog | |
*/ | |
#include <stdio.h> | |
#include <time.h> | |
int main(void) { | |
// Tiempo actual | |
time_t t = time(NULL); | |
struct tm tiempoLocal = *localtime(&t); | |
// El lugar en donde se pondrá la fecha y hora formateadas | |
char fechaHora[70]; | |
// El formato. Mira más en https://en.cppreference.com/w/c/chrono/strftime | |
char *formato = "%Y-%m-%d %H:%M:%S"; | |
// Intentar formatear | |
int bytesEscritos = | |
strftime(fechaHora, sizeof fechaHora, formato, &tiempoLocal); | |
if (bytesEscritos != 0) { | |
// Si no hay error, los bytesEscritos no son 0 | |
printf("Fecha y hora: %s", fechaHora); | |
} else { | |
printf("Error formateando fecha"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment