如何比较系统日期和服务器日期 [英] How to compare system date and server date

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

问题描述

我需要根据行更新来比较系统日期和服务器日期。

我尝试了一些东西,但它只适用于localhost,而不是服务器上



我尝试过:



I need to compare system date and server date based on that row update.
I have tried something but it work only on localhost,not on server

What I have tried:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

       string userid = GVmydsr.DataKeys[e.RowIndex].Value.ToString();
       GridViewRow row = (GridViewRow)GVmydsr.Rows[e.RowIndex];
       Label date = (Label)row.FindControl("lblcurrentdate");

       string cdate = date.Text.ToString();
       DateTime date1 = DateTime.MinValue;
       DateTime.TryParseExact(cdate.Split(' ')[0], new string[] {"dd-MM-yyyy"}, null, System.Globalization.DateTimeStyles.None, out date1);
       string dt1 = date1.ToString("dd-MM-yyyy 00:00:00");

       GVmydsr.EditIndex = -1;




string dt2 = DateTime.Today.ToString("dd-MM-yyyy 00:00:00");

        if (dt1 == dt2)
        {
            con.Open();
           //My code
            cmd.ExecuteNonQuery();

        }

推荐答案

为什么使用字符串进行比较而不是DateTime值?为什么要将DateTime值转换为字符串并再次返回多次?



从网格中获取值,并将其解析为DateTime,除非它已经是以这种方式存储。然后,您可以将日期部分与DateTime.Now进行比较。

Why are you using strings to compare, instead of DateTime values? And why are you converting DateTime values to strings and back again so many times?

Get the value from your grid, and parse it to a DateTime, unless it is already stored in that way. You can then compare the date portion with DateTime.Now.
if (dt1.Date == DateTime.Now.Date)
// ...


引用:

我做了更改,但它只在localhost上工作而不在服务器上..

i made change but it work only on localhost not on server..



您的服务器和客户端可能处于不同的时区:按理查德建议并使用DateTime值,但在将所有日期和时间值存储到数据库之前将其转换为UTC,并且在进行任何比较之前。 DateTime c =有一个方法( DateTime.ToUniversalTime Method (系统) [ ^ ])但是您需要每次都找出客户所在的时区 - 您不能只使用服务器TZ在服务器上转换它 - 请记住,客户可能不会和他们在同一时区!



我?我会在服务器上以UTC格式进行所有DateTime标记,并尽可能忽略客户端值。


The chances are that your server and client are in different time zones: do as Richard suggests and use DateTime values, but convert all your date and time values to UTC before you store them in your DB, and before you do any comparisons. DateTime c=has a method for this (DateTime.ToUniversalTime Method (System)[^]) but you will need to find out each time what timezone the Client is in - you can't just convert it on the server using the server TZ - remember that c lients may not be in the same time zone as each other!

Me? I'd do all DateTime stamping at the server, in UTC and ignore client values wherever possible.


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

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