+3 votes
49 views

Hello Guys! I have been working in Web Development field for many years. I use php to develop the back-end of websites. For some days, I am working in C++ to develop Database Management systems in c++ but I am unaware of its most syntax and functions. So, The question I want to ask is there a funtion that returns current date and time in c++? Actually, in php, we use the following functions to get date and time

<?php
$time = time();

$date = date("d/m/y", $time);

echo $date;
?>

I want a such like method in c++. The I.D.E I am using is Dev C++

Thanks in advance!

by (3.2k points)   | 49 views

1 Answer

0 votes

Hi! I looked at your question and come to know that it is so easy way to get current date in php but unfortunately it is not easy in C++. You have to do much mor work to get current date and time and make it human readable. By the way, here is how to get current date and time in c++

#include<iostream>
#include<time.h>
using namespace std;

int main(){

    //This will return current time in seconds
    time_t STime = time(NULL);
    //tm is a predefined data structure
    tm* tPointer = localtime(&STime);

    //The date and time is now stored into the data structure tm
    cout<<"-----Solution by M.Athar-----\n";
    cout<<"Day: " << tPointer->tm_mday <<endl;
    cout<<"Month: " << (1 + tPointer->tm_mon) <<endl; //Add +1
    cout<<"Year: " << (1900 + tPointer->tm_year) <<endl; //Add 1900
    cout<<"Time: " << tPointer->tm_hour << ":" << tPointer->tm_min << ":" << tPointer->tm_sec << endl; //Hours:Minutes:Seconds 
    cout<<"\n-----Solution by M.Athar-----";
}

image.png

Hope it will work for you! Regards, M.Athar

by (1.3k points)  
Wao great!
It is perfect for my bs.se assignment

Related questions

+2 votes
1 answer
+1 vote
1 answer
asked Jun 13 by noor (1.1k points) | 15 views
+1 vote
0 answers
+2 votes
1 answer
asked Jun 11 by noor (1.1k points) | 20 views
+2 votes
0 answers
asked Jun 20 by athar (3.2k points) | 10 views
+1 vote
1 answer
asked Jun 11 by noor (1.1k points) | 13 views
+1 vote
0 answers
+2 votes
1 answer
asked Jun 5 by toheed (1.3k points) | 16 views
0 votes
1 answer
asked Jul 2 by toheed (1.3k points) | 37 views




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
117 questions
88 answers
42 comments
106 users