1//Return the time from DateTime object in string format
2var timeString = DateTime.Now.ToString("hh:mm:ss");
3
4//Return time in 24h format
5var time24 = DateTime.Now.ToString("HH:mm:ss");
6
7//Use short time format to return string value
8var timeString = DateTime.Now.ToString("t");
9var shortTimeStr = DateTime.Now.ToShortTimeString();
10
11//Use long time format
12var longTimeStr = DateTime.Now.ToLongTimeString();
13var longtimestr = DateTime.Now.ToString("T");
14
15//Return a TimeSpan from midnight
16var timeSpan = DateTime.Now.TimeOfDay;
1/* localtime example */
2#include <stdio.h>
3#include <time.h>
4
5int main ()
6{
7 time_t rawtime;
8 struct tm * timeinfo;
9
10 time ( &rawtime );
11 timeinfo = localtime ( &rawtime );
12 printf ( "Current local time and date: %s", asctime (timeinfo) );
13
14 return 0;
15}
16
1#include <time.h>
2#include <stdlib.h>
3#include <stdio.h>
4
5int main(void)
6{
7 time_t current_time;
8 char* c_time_string;
9
10 /* Obtain current time. */
11 current_time = time(NULL);
12
13 if (current_time == ((time_t)-1))
14 {
15 (void) fprintf(stderr, "Failure to obtain the current time.\n");
16 exit(EXIT_FAILURE);
17 }
18
19 /* Convert to local time format. */
20 c_time_string = ctime(¤t_time);
21
22 if (c_time_string == NULL)
23 {
24 (void) fprintf(stderr, "Failure to convert the current time.\n");
25 exit(EXIT_FAILURE);
26 }
27
28 /* Print to stdout. ctime() has already added a terminating newline character. */
29 (void) printf("Current time is %s", c_time_string);
30 exit(EXIT_SUCCESS);
31}
32