i wrote the code to Fetch date and time from server not from client system. i write like this...
DateTime dt = new 开发者_开发技巧DateTime();
dt = DateTime.Now;
is this correct code?
Invoking the DateTime
constructor is not necessary. Use this instead:
var dt = DateTime.Now;
(This is equivalent, though slightly less verbose, than DateTime dt = DateTime.Now
)
yes, use the DateTime.Now to obtain the current date and time on the machine where the program resides (in the case of ASP.NET the server).
You could use:
DateTime time = DateTime.Now;
string format = "MMM ddd d HH:mm yyyy";
var dt = time.ToString(format);
Makes it aesthetically pleasing. You can format with:
MMM display three-letter month
ddd display three-letter day of the WEEK
d display day of the MONTH
HH display two-digit hours on 24-hour scale
mm display two-digit minutes
yyyy display four-digit year
Of course you can use the NTP source namespace from Google. It's some source you can download: http://www.google.com/codesearch/p?hl=en&sa=N&cd=1&ct=rc#GawBxmf1je8/NTP/NtpClient.cs&q=ntp%20lang:c%23&l=34
To get server time you need to make a class and need to modify it by server connection.
var my_date = ServerDateTime.GetServerDatetime().Date;
Get local time
var my_date = DateTime.Now;
精彩评论