以小时和分钟计算两次之间的时差 [英] Calculate time difference between two times in hrs and minutes

查看:71
本文介绍了以小时和分钟计算两次之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要计算两次之间的时差。上午和下午的时间来自两个下拉列表。它在几小时的情况下正常工作。但是当涉及到差30分钟不起作用。



例如:



开始时间:10:3​​0 AM

结束时间:11:00 AM



O / P:0:30



如何实现这一目标。任何帮助都会非常感激。谢谢。



我尝试了什么:



Hi All,
I need to calculate time difference between two times.The times with AM and PM are taken from two dropdownlists .It is working correctly in case of hours.But when it comes to 30 mins difference its not working.

For example :

Start time : 10:30 AM
End Time : 11:00 AM

O/P : 0:30

How to achieve this .Any help will be really appreciated.Thanks in Advance

What I have tried:

int.Parse(objcon.GetDataReadervalue("select datediff(HOUR,'" + ddl_fromtime.SelectedValue.ToString() + "','" + ddl_totime.SelectedValue.ToString() + "') ", "Value"))

推荐答案

首先关闭:不要这样做那!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

First off: don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



当你在整个应用程序中修复它时,你可以解决这个问题,这是非常重要的:做的DATEDIFF的分钟数,而不是小时数。然后你可以用几小时和几分钟(如果你需要,可能你没有)使用除数和模数60。

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

When you have fixed that throughout your app, you can work on this problem, which is pretty trivial: do the DATEDIFF in terms of minutes, not hours. You can then work that into hours and minutes (if you need to, probably you don't) using divide and modulus 60.


你可以尝试他的代码,这可能会帮助你。< br $>


you could try his code this might help you out.

var date1 = DateTime.Now.AddMinutes(30);  // it will add 30 minutes of time of the current time 
            var date2 = DateTime.Now;
            var diff = date1 - date2;
            var diffMinutes = date1.Minute - date2.Minute;
            Console.WriteLine("date difference is : " + diff); 
            Console.WriteLine("Minutes difference is : " + diffMinutes); 
            Console.ReadLine();


这篇关于以小时和分钟计算两次之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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