← Back to C Course | Chapter 14: Additional Topics | Lesson 7 of 9

C में Date और Time

C computer से current date और time मांग सकता है और इसे readable text में बदल सकता है, एक calendar और clock combined की तरह। आप इसे day, month और year में भी तोड़ सकते हैं।
Syntax
c
#include <time.h>

time_t now = time(NULL);   // seconds since the Unix epoch
printf("%s", ctime(&now));   // human-readable text
struct tm *info = localtime(&now);

time.h Header

time.h header वे core types और functions supply करता है जो C calendar dates और times के साथ काम करने के लिए इस्तेमाल करता है, सबसे notably समय के एक single point represent करने के लिए time_t type और उसी point को year, month, और day जैसे individual fields में तोड़ने के लिए struct tm type।

उदाहरण: The time.h Header

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
	time_t now;
	struct tm *info;
	// Print formatted text to the screen
	printf("time.h provides time_t and struct tm");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Current Time पाना

time() current calendar time को एक time_t value की तरह return करता है, Unix epoch (आधी रात, 1 जनवरी, 1970 UTC) से बीते seconds की संख्या मापते हुए — एक single portable number जिसे दूसरे time.h functions फिर ज़्यादा human-friendly forms में convert कर सकते हैं।

उदाहरण: Getting Current Time

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
	time_t now = time(NULL);
	// Print formatted text to the screen
	printf("%d", now > 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

ctime() से Format करना

ctime() एक raw time_t value को day of the week, month, date, time of day, और year रखने वाली एक ready-made, human-readable string में convert करता है, आपको उन pieces को खुद manually assemble करने से बचाते हुए, हालांकि इसका exact output format fixed है और customizable नहीं।

उदाहरण: Formatting with ctime()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
	time_t now = time(NULL);
	// Print formatted text to the screen
	printf("%s", ctime(&now));
	// Return 0 to signal that the program finished successfully
	return 0;
}

localtime() से Dates Deconstruct करना

localtime() एक time_t value लेता है और इसे year, month, day, hour, minute, और second (सब local time zone के लिए adjusted) के लिए separate fields वाले एक struct tm में expand करता है, जो आपको तब चाहिए जब आप पूरी चीज़ की बजाय किसी date के सिर्फ एक piece को inspect या इसके साथ काम करना चाहें।

उदाहरण: Deconstructing Dates with localtime()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
	time_t now = time(NULL);
	struct tm *local = localtime(&now);
	// Print formatted text to the screen
	printf("%d", local->tm_year + 1900);
	// Return 0 to signal that the program finished successfully
	return 0;
}

strftime() से Custom Formats

strftime() printf जैसे format specifiers इस्तेमाल करके एक custom-formatted date string बनाता है, जैसे चार-digit year के लिए %Y या पूरे month name के लिए %B, ctime() के single fixed layout तक सीमित होने की बजाय एक date के displayed format पर पूरा control देते हुए।

उदाहरण: Custom Formats with strftime()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
	time_t now = time(NULL);
	struct tm *local = localtime(&now);
	char buffer[30];
	strftime(buffer, 30, "%Y-%B", local);
	// Print formatted text to the screen
	printf("%s", buffer);
	// 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 या localtime call करने से पहले #include <time.h> भूल जाना।
  2. localtime से pointer store करना और इसे दोबारा इस्तेमाल करना, क्योंकि यह एक static buffer की ओर point करता है।
  3. यह भूल जाना कि tm_year 1900 से count करता है और tm_mon 0 से।

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.