c#如何正确地将字符串转换为在TotalHours中使用的日期时间 [英] c# how to properly convert a string to a date time to be use in TotalHours

查看:453
本文介绍了c#如何正确地将字符串转换为在TotalHours中使用的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有asp.net c#

计算总小时数。

因为我将从数据库中提取数据作为字符串值,我必须转换该字符串到一个似乎根本不起作用的日期时间。



值2015,4,12,22,28,20必须是一个字符串(我将从中提取数据)稍后计划中的数据库)。并将该字符串放入new DateTime(stringvalue)



看看我的代码



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;



命名空间WebApplication13

{

public partial class _Default:System.Web.UI.Page

{

DateTime now;

// string date =2015,4,12,22,28,20;

protected void Page_Load(object sender,EventArgs e)

{

now = new DateTime(2015,4,12,22,28,20); //年,月,日,小时,分钟,秒

// now = Convert.ToDateTime(date);

}



protected void Button1_Click(对象发送者,EventArgs e)

{

Label3.Text = DateTime.Now.ToString();

Label2.Text = now.ToString();

TimeSpan diff = DateTime.Now - now;

double hours = diff.TotalHours;

Label1.Text =总小时数为:+ hours.ToString();

}

}

}

i have asp.net c#
computing total number of hours.
since i will pull the data from a database as a string value, i must convert that string to a datetime which seems not working at all.

the value 2015, 4, 12, 22, 28, 20 must be a string ( i will pull the data from a database in a later plan). and put that string inside "new DateTime(stringvalue)"

take a look at my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication13
{
public partial class _Default : System.Web.UI.Page
{
DateTime now;
//string date = "2015, 4, 12, 22, 28, 20";
protected void Page_Load(object sender, EventArgs e)
{
now = new DateTime(2015, 4, 12, 22, 28, 20); // year, Month, day, hours, minutes, seconds
//now = Convert.ToDateTime(date);
}

protected void Button1_Click(object sender, EventArgs e)
{
Label3.Text = DateTime.Now.ToString();
Label2.Text = now.ToString();
TimeSpan diff = DateTime.Now - now;
double hours = diff.TotalHours;
Label1.Text = "total number of hours is: " + hours.ToString();
}
}
}

推荐答案

不要。



永远不要将DateTime值作为字符串存储在数据库中:在输入点解析它们(或尽可能接近),因为只有这样才能告诉用户输入它们的格式,并且它们是正确。他们到达数据库时已经太晚了,因为他们丢失了所有用户上下文,如果他们错了:第74个Smegruary 4215,拒绝数据并正确输入数据为时已晚。因为你失去了所有的背景,你不知道01/02/03是什么日期!此时,不良数据存在于数据库中,并且每次尝试使用时都会给您带来问题。



将数据存储为最合适的数据类型,而不是一个对你来说最简单!它稍后节省了大量的时间和精力。



使用字符串存储意味着你也连接字符串以形成你的SQL命令 - 这是非常的,非常危险,因为它允许用户执行可能损坏或破坏您的数据库的SQL注入攻击。始终使用参数化查询!
Don't.

Never store DateTime values in a DB as a string: parse them at point of input (or as close as you can get) because only then can you tell what format the user has input them in, and that they are correct. It's far too late by the time they get to the database as they have lost all user context, and if they are wrong: "74th Smegruary 4215" it's too late to reject the data and get it entered correctly. And since you lost all context, you don't know what date "01/02/03" is either! At this point, the bad data is in the DB and will give you problems every time you try to use it.

Store data as the most appropriate datatype, not the one which is easiest for you! It saves a heck of a lot of time and effort later.

And the use of string storage implies that you also concatenate strings to form your SQL commands - which is very, very dangerous as it allows the user to perform SQL Injection attacks which can damage or destroy your database. Always use parametrised queries!


您可以尝试 DateTime.Parse()方法及其所有重载。



DateTime.Parse Method [ ^ ]



DateTime.TryParse 也可以是一个选项。
You can try DateTime.Parse() method with all its overloads.

DateTime.Parse Method[^]

DateTime.TryParse can be an option, too.


这篇关于c#如何正确地将字符串转换为在TotalHours中使用的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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