Mana
Mana

Reputation: 1929

where does datetime get its data from asp.net / c#

I was wondering, where does

DateTime.Now;

get its data from?

does it get the data from my desktop? actually it does when i'm observing under debugging. I changed my desktop time back an hour, and the datetime.now gave me the exact same date/time.

My real question is, will this still apply when the project is deployed? Will my site continue to fetch time from the users pcs?

The reason for this question is, because i will be using session variables, which i want to dispose after x minutes. And if datetime.now gets the time from the users computer, then it would be a kind of a security issue.

Thanks in advance for your answer.

Upvotes: 2

Views: 1007

Answers (4)

rahul.deshmukh
rahul.deshmukh

Reputation: 590

In asp.net or c# DateTime.Now shows time of server where the application is running.

Upvotes: 1

Gaurav
Gaurav

Reputation: 221

You need to keep your server Date correctly. As your code will run on server you will keep on getting the correct time. Changing the client machine date won't affect the server time. All your code behind (server side code) will get the correct time.

Upvotes: 1

Amit
Amit

Reputation: 1885

Your analysis is both right and wrong. DateTime.Now would get the time from the machine where the code is running on. I assume that you have that piece on ASP scripts, that would be running on the web server when you deploy them. So the time would be of the web server rather than user's PC.

You could get the time from User PCs through client side scripts; like JS.

Upvotes: 3

Marc Gravell
Marc Gravell

Reputation: 1063058

ASP.NET runs at the server. It is getting the time at the server. Note that during development, the server often happens to be the "user's PC", but that is coincidence.

Upvotes: 7

Related Questions