Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want get the date time
but i writed this code
example ;
system.datetime.now.day
and it geted me day of my system
i want date of server (realy time)
Posted
Comments
#realJSOP 7-Mar-12 10:05am    
What kind of application? Is the server on the LAN?
sadegh_rusta 10-Mar-12 8:54am    
server!!!

1 solution

C#
DateTime today = DateTime.Now;
This gets the current date and time into the variable today - you can then use what parts of it you need:
today.Day is the day of the month,
today.Month is the month of the year,
today.Hour is the hour of the day,
today.Minute is the minute of the hour.

Probably, you want to convert this to a string for display, so use the ToString method:
C#
string timeIn12HourClock = today.ToString("hh:mm");
string timeIn24HourClock = today.ToString("HH:mm");


There are load of other formats you can use or construct: Formatting a DateTime for display - format string description[^]
 
Share this answer
 
Comments
sadegh_rusta 7-Mar-12 11:50am    
no no
i want know if a write this code for my site
and after upload my site on a server
this code is correct?
this mean that this code return date time then server?
OriginalGriff 7-Mar-12 12:25pm    
Yes. DateTime.Now always returns the time as per the clock on the computer on which it executes.
So since the C# executes in the codebehind, it executes on the server so it returns server time, not client time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900