在system.dateTime.subtract上进行以下编辑 [英] Getting the following edit on a system.dateTime.subtract
问题描述
我收到此错误:
I am getting this error:
System.DateTime.Subtract(System.DateTime)' is a 'method', which is not valid in the given context
我的代码是:
my code is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
double age = Math.Round(System.DateTime.Now.Subtract.Tostring(birthDate.Text).TotalDays * 365.25, 2);
if (age < 21)
validationResult.Text = "You are under 21.";
else
validationResult.Text = "You are 21 or older.";
}
}
我对此还比较陌生.知道如何解决吗?
好的,我感谢收到的答复,但老实说,这根本没有帮助.这是我必须使用的代码.我应该找出问题所在并加以解决.我能够解决其他5个问题,但无法弄清楚如何解决此问题.给出的这些解决方案将不适用于该解决方案.我只需要知道如何摆脱这一修改.我尝试在其中扔一些(),但没有做任何工作.我疯狂地搜寻课本和互联网.有人可以告诉我编辑内容在此行中显示吗?
双倍年龄= Math.Round(System.DateTime.Now.Subtract.Tostring(birthDate.Text).TotalDays * 365.25,2);
有人可以直接告诉我该放在哪里吗.
I am relatively new to this. Any idea how to fix?
ok I appreciate the responses i have gotten but honestly that was no help at all. This is code that i have to use. I am supposed to find what is wrong with it and fix it. I was able to fix the other 5 problems but cant figure out how to fix this one. These solutions given will not work for that. I just needed to know how to get rid of this one edit. I tried throwing some () in there but nothing i did worked. I have scoured the text book and internet like crazy. Can someone just tell me the edit is showing on this line:
double age = Math.Round(System.DateTime.Now.Subtract.Tostring(birthDate.Text).TotalDays * 365.25, 2);
can someone just tell me straight forward what to put where. I would really appreciate it.
推荐答案
请注意,TimeSpan对象为您提供了一些有用的属性来确定持续时间.例如:
Note that the TimeSpan object gives you several useful properties for determining durations; example:
DateTime now = DateTime.Now;
// note you can use the subtract operator - directly
// and use a negative value with 'AddDays
TimeSpan difference = now - (now.AddDays(-2.4));
// days as a whole number
int diffDays = difference.Days;
// days including fractional value
double totalDays = difference.TotalDays;
int diffHours = difference.Hours;
double totalHours = difference.TotalHours;
我建议阅读此提示:使用年龄:与时间跨度不同! [ ^ ]
I''d suggest to read this tip: Working with Age: it''s not the same as a TimeSpan![^]
要获取两个日期之间的差值,我们可以使用减法"方法.这是参数化的方法.在此处检查.
而不是像这样使用它:
To get the difference between two dates we can uses the "Subtract" method. It is a parameterised method.Check Here.
Instead of using it like:
double age = Math.Round(System.DateTime.Now.Subtract.Tostring(birthDate.Text).TotalDays * 365.25,2) ;
double age = Math.Round(System.DateTime.Now.Subtract.Tostring(birthDate.Text).TotalDays * 365.25, 2);
将其用作:
use it as:
double age = Math.Round(DateTime.Today.Subtract(new DateTime(1990, 07, 16)).TotalDays / 365.25, 2);
希望对您有所帮助:)
Hope it helps:)
这篇关于在system.dateTime.subtract上进行以下编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!