Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active July 31, 2019 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/37c1ddfc6a7c70028b6675d7717f5914 to your computer and use it in GitHub Desktop.
Save parzibyte/37c1ddfc6a7c70028b6675d7717f5914 to your computer and use it in GitHub Desktop.
/*
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