Imprimir la hora del sistema en C++ (3 formas diferentes)

Primer método
Imprimiendo la fecha y hora actual usando time()

segundo método

// CPP program to print current date and time
// using time and ctime.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  
int main()
{
    // declaring argument of time()
    time_t my_time = time(NULL);
  
    // ctime() used to give the present time
    printf("%s", ctime(&my_time));
    return 0;
}

Producción:

It will show the current day, date and localtime, 
in the format Day Month Date hh:mm:ss Year

Tercer método
Aquí hemos utilizado la biblioteca crono para imprimir la fecha y hora actuales. La biblioteca crono es una colección flexible de tipos que realiza un seguimiento del tiempo con diversos grados de precisión.
La biblioteca crono define tres tipos principales, así como funciones de utilidad y definiciones de tipos comunes.
-> relojes
-> puntos de tiempo
-> duraciones

Código para imprimir fecha, día y hora actuales.

// CPP program to print current date and time
// using chronos.
#include <chrono>
#include <ctime>
#include <iostream>
  
using namespace std;
  
int main()
{
    // Here system_clock is wall clock time from
    // the system-wide realtime clock
    auto timenow =
      chrono::system_clock::to_time_t(chrono::system_clock::now());
  
    cout << ctime(&timenow) << endl;
}

Producción:

It will show the current day, date and localtime, 
in the format Day Month Date hh:mm:ss Year

Publicación traducida automáticamente

Artículo escrito por Surya Priy y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *