Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the current time and date in C#

People also ask

How do I get the current time in C?

C Program to Display the current Date and Time The C library function char *ctime(const time_t *timer) returns a string representing the localtime based on the argument timer. The returned string has the following format: Www Mmm dd hh:mm:ss yyyy.

How do I print the time in printf?

printf("The time is: %02d:%02d:%02d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); We use the tm_hour , tm_min , and tm_sec members to express a human-readable time format.

How will you print the date in below format using C?

One way to check this: Just initialize the variable and then take input. int date = 0, month = 0, year = 0; scanf("%d/%d/%d", &date, &month, &year); Now, if you give 10-12-2016 as input, you will get 10-0-0 as output.


You'd need to set the label's text property to DateTime.Now:

labelName.Text = DateTime.Now.ToString();

You can format it in a variety of ways by handing ToString() a format string in the form of "MM/DD/YYYY" and the like. (Google Date-format strings).


The System.DateTime class has a property called Now, which:

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.

You can set the Text property of your label to the current time like this (where myLabel is the name of your label):

myLabel.Text = DateTime.Now.ToString();

For time:

label1.Text = DateTime.Now.ToString("HH:mm:ss"); //result 22:11:45

or

label1.Text = DateTime.Now.ToString("hh:mm:ss tt"); //result 11:11:45 PM

For date:

label1.Text = DateTime.Now.ToShortDateString(); //30.5.2012

labelName.Text = DateTime.Now.ToString("dddd , MMM dd yyyy,hh:mm:ss");

Output:

][1


DateTime.Now.Tostring();

. You can supply parameters to To string function in a lot of ways like given in this link http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

This will be a lot useful. If you reside somewhere else than the regular format (MM/dd/yyyy)

use always MM not mm, mm gives minutes and MM gives month.


If you want to do it in XAML,

xmlns:sys="clr-namespace:System;assembly=mscorlib"
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}}"

With some formatting,

<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now},
                  StringFormat='{}{0:dd-MMM-yyyy hh:mm:ss}'}"