Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am getting this error:

System.DateTime.Subtract(System.DateTime)' is a 'method', which is not valid in the given context


my code is :

C#
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.";
    }
}


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.
Posted
Updated 26-Oct-14 6:35am
v3
Comments
[no name] 25-Oct-14 16:36pm    
Just what is says. Subtract is a method so you need to throw some () in there.

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:
Quote:
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:)
 
Share this answer
 
I'd suggest to read this tip: Working with Age: it's not the same as a TimeSpan![^]
 
Share this answer
 
Comments
BillWoodruff 25-Oct-14 19:40pm    
+5 That's a fascinating article by OG; did you note the very recent comments on problems in the code by eagle-eyed Richard Deeming ?

Note that OG's code does not compute the total age in Months, Days, etc.
Maciej Los 26-Oct-14 5:14am    
Thank you, Bill ;)
Yeah, you're right, Richard Deeming is very observant. His new nick should be: Richard "Hawk Eye" Deeming ;)
Note that the TimeSpan object gives you several useful properties for determining durations; example:
C#
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;
 
Share this answer
 
Comments
Maciej Los 25-Oct-14 18:04pm    
+5
BillWoodruff 25-Oct-14 19:35pm    
Thank you, Maciej
Sergey Alexandrovich Kryukov 26-Oct-14 1:41am    
The usage of the API you show is correct, but did you pay attention that difference is always 2.4 days? Why calculating the constant? :-)
—SA
[no name] 26-Oct-14 19:44pm    
You got my 5+ on this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900