如何获取系统日期时间而不是服务器。 [英] How to get system date time instead of server.

查看:341
本文介绍了如何获取系统日期时间而不是服务器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在本地主机上运行我的应用程序时,我从DateTime.Now获取系统时间和日期。

但是当我在服务器上托管它时,我得到了服务器日期时间。这是Godaddy服务器。(美国)。而不是服务器日期时间我想要检索机器时间和日期



我尝试过:



DateTime dateValue = DateTime.Now;

dateValue.ToString(dddd);

+ dateValue.ToString(h:mm:ss tt);

When I run my application on local host I get my system time and date from DateTime.Now.
But when I hosted it on server I am getting the server Date Time. It is Godaddy server.(USA). instead of server datetime I want to retrieve the machine time and date

What I have tried:

DateTime dateValue = DateTime.Now;
dateValue.ToString("dddd");
+dateValue.ToString("h:mm:ss tt");

推荐答案

基本上有两种方法 -

1。客户端方法

使用这种方法,您可以获得一些客户端脚本(如javascript)的帮助,以获取客户端系统上的日期和时间。类似于以下内容 -

Basically there are two approaches-
1. Client side approach
With this approach you can take help of some client script like javascript to get the date and time on the client system. Something like following-
var currentdate = new Date();



检查以下链接 -

JavaScript日期 [ ^ ]



好​​的。如果我的电脑设置错误了怎么办?错误的结果是期待的。这就是我们需要第二种选择的地方。

2。将时间转换为本地

嗯,您可以使用C#代码执行此操作。

示例:


Check following link-
JavaScript Dates[^]

Ok. Now what if I have wrong time set in my computer? Wrong result to expect. That's where we need the second option.
2. Convert the time to local
Well, you can do this with your C# code.
Example:

DateTime dateValue = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")); //for converting to IST





希望,它有帮助:)

注意:在客户端也有办法做到这一点。但这两个选项应该有助于你的事业。如果我在这里遗漏了什么,请告诉我。



Hope, it helps :)
Note: There are ways to do this in client side too. but these 2 options should help your cause. Please let me know in case I am missing something here.


这篇关于如何获取系统日期时间而不是服务器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆