C++ में Date और Time
In this page:
#include <ctime>
time_t now = time(0);
ctime(&now); // readable date string
Current Time पाना
<ctime> से time() function current time को January 1, 1970 से बीते seconds की संख्या के रूप में return करता है, commonly Unix time या epoch time कहा जाता है।
उदाहरण: Getting the Current Time
// Include std::cout and std::cin
#include <iostream>
#include <ctime>
// Program execution starts in main()
int main() {
time_t now = time(0);
// Print to the console with cout
std::cout << "Seconds since epoch: " << now << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Readable Date में Convert करना
ctime() एक time_t value को एक human-readable string में convert करता है जो day, date, और time दिखाती है, raw epoch number को एक नज़र में समझने लायक बनाते हुए।
उदाहरण: Converting to a Readable Date
// Include std::cout and std::cin
#include <iostream>
#include <ctime>
// Program execution starts in main()
int main() {
time_t now = time(0);
// Print to the console with cout
std::cout << ctime(&now);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
struct tm से एक Date Break Down करना
localtime() एक time_t value को एक struct tm में break down करता है, year, month, day, hour, और minute जैसे individual fields direct access के लिए expose करते हुए।
उदाहरण: Breaking Down a Date with struct tm
// Include std::cout and std::cin
#include <iostream>
#include <ctime>
// Program execution starts in main()
int main() {
time_t now = time(0);
tm *local = localtime(&now);
// Print to the console with cout
std::cout << "Year: " << (local->tm_year + 1900) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
strftime से Dates Format करना
strftime() printf जैसे format specifiers इस्तेमाल करते हुए एक struct tm को एक custom string में format करता है, एक date और time exactly कैसे display हों इस पर पूरा control देते हुए।
उदाहरण: Formatting Dates with strftime
// Include std::cout and std::cin
#include <iostream>
#include <ctime>
// Program execution starts in main()
int main() {
time_t now = time(0);
tm *local = localtime(&now);
char buffer[50];
strftime(buffer, sizeof(buffer), "%Y-%m-%d", local);
// Print to the console with cout
std::cout << buffer << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Elapsed Time Measure करना
दो time_t values subtract करना, या difftime() इस्तेमाल करना, calculate करता है कि time में दो points के बीच कितने seconds बीते, durations या program run time measure करने के लिए useful।
उदाहरण: Measuring Elapsed Time
// Include std::cout and std::cin
#include <iostream>
#include <ctime>
// Program execution starts in main()
int main() {
time_t start = time(0);
time_t end = start + 5;
// Print to the console with cout
std::cout << difftime(end, start) << " seconds elapsed" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
time()याstrftimecall करने से पहले#include <ctime>भूल जाना।localtimeसे returned pointer store करना और बाद में इस्तेमाल करना, क्योंकि यह एक shared static buffer की ओर point करता है जिसे अगला call overwrite कर देता है।strftimeको एक बहुत छोटा buffer pass करना, जो फिर 0 return करता है और buffer को unusable छोड़ देता है।
- File handling programs को
ifstream,ofstream, औरfstreamसे files पढ़ने और लिखने देती है। - Binary files raw data store करती हैं, और error handling यह check करती है कि file operations succeed हों।
- Date और time facilities programs को time के साथ काम करने देती हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: