Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,821 answers

573 users

How to get the beginning and end of the day in C++

2 Answers

0 votes
#include <iostream>
#include <chrono>
#include <format>   // C++20

using namespace std;
using namespace std::chrono;

int main() {
    // Get current system time
    auto now = system_clock::now();

    // Convert to days precision
    auto today = floor<days>(now);

    // Beginning of the day (00:00:00)
    auto start_of_day = today;

    // End of the day (23:59:59.999...)
    auto end_of_day = today + days{1} - milliseconds{1};

    std::cout << "Start: " << std::format("{:%FT%T}", start_of_day) << "Z\n";
    std::cout << "End:   " << std::format("{:%FT%T}", end_of_day) << "Z\n";
}




/*
run:

Start: 2025-12-12T00:00:00Z
End:   2025-12-12T23:59:59.999Z

*/

 



answered Dec 12, 2025 by avibootz
0 votes
#include <iostream>
#include <ctime>

int main() {
    std::time_t now = std::time(nullptr);
    std::tm local_tm = *std::localtime(&now);

    // Beginning of the day
    local_tm.tm_hour = 0;
    local_tm.tm_min  = 0;
    local_tm.tm_sec  = 0;
    std::time_t start_of_day = std::mktime(&local_tm);

    // End of the day
    local_tm.tm_hour = 23;
    local_tm.tm_min  = 59;
    local_tm.tm_sec  = 59;
    std::time_t end_of_day = std::mktime(&local_tm);

    std::cout << "Start: " << std::asctime(std::localtime(&start_of_day));
    std::cout << "End:   " << std::asctime(std::localtime(&end_of_day));
}



/*
run:

Start: Fri Dec 12 00:00:00 2025
End:   Fri Dec 12 23:59:59 2025

*/

 



answered Dec 12, 2025 by avibootz
...