← Back to C++ Course | Chapter 15: File I/O | Lesson 6 of 6

C++ में Date और Time

<ctime> library C++ में dates और times के साथ काम करने के लिए time() और localtime() जैसे functions देती है, एक program को current moment capture और display के लिए format करने देते हुए।
Syntax
cpp
#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

cpp
// 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;
}

एक Readable Date में Convert करना

ctime() एक time_t value को एक human-readable string में convert करता है जो day, date, और time दिखाती है, raw epoch number को एक नज़र में समझने लायक बनाते हुए।

उदाहरण: Converting to a Readable Date

cpp
// 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;
}

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

cpp
// 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;
}

strftime से Dates Format करना

strftime() printf जैसे format specifiers इस्तेमाल करते हुए एक struct tm को एक custom string में format करता है, एक date और time exactly कैसे display हों इस पर पूरा control देते हुए।

उदाहरण: Formatting Dates with strftime

cpp
// 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;
}

Elapsed Time Measure करना

दो time_t values subtract करना, या difftime() इस्तेमाल करना, calculate करता है कि time में दो points के बीच कितने seconds बीते, durations या program run time measure करने के लिए useful।

उदाहरण: Measuring Elapsed Time

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. time() या strftime call करने से पहले #include <ctime> भूल जाना।
  2. localtime से returned pointer store करना और बाद में इस्तेमाल करना, क्योंकि यह एक shared static buffer की ओर point करता है जिसे अगला call overwrite कर देता है।
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.