C में Date और Time
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
timeयाlocaltimecall करने से पहले#include <time.h>भूल जाना।localtimeसे pointer store करना और इसे दोबारा इस्तेमाल करना, क्योंकि यह एक static buffer की ओर point करता है।- यह भूल जाना कि
tm_year1900 से count करता है औरtm_mon0 से।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: