Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
DateTime datenow = DateTime.Now;
int age=0;
age =Convert.ToInt32(ddlyear.SelectedItem.Text).ToString();
int result = 0;
result =Convert.ToDateTime(datenow).ToString() - Convert.ToInt32(age).ToString();
txtage.Text = result.ToString();
Posted 12-Jan-13 20:19pm
Edited 12-Jan-13 20:51pm
v2
Comments
jibesh at 13-Jan-13 2:41am
   
what are you trying to do? calculate the age difference?
Dharmendra-18 at 13-Jan-13 2:44am
   
can you elaborate your question for better solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It looks like your trying to get the age in years using DateTime.Now and the birth date.
 
This will do just that.
 
DateTime today = DateTime.Today;
 
int calc1 = (today.Year * 100 + today.Month) * 100 + today.Day;
int calc2 = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day;
 
int ageInYears = (a - b) / 10000;
 
txtage.Text = ageInYears.ToString();
 
But since the title of your question is "How i convert dataetime to int and string to Int" this is the following
 
//Convert DateTime To String
DateTime now = DateTime.Now;
string nowString = now.ToShortDateString();
//Convert String To Int
int value1 = 22;
string value1Converted = value1.ToString();
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The code has serious issues it wont even compile.You are totally confused with the conversions. I would suggest understand the basics of assignment operations too.
 
you are trying to store a string data into an integer variable 'age' at the following location which is wrong you never store a string into an int.
//wrong code
age =Convert.ToInt32(ddlyear.SelectedItem.Text).ToString(); //compiler error
//Correct Code
age = Convert.ToInt32(ddlyear.SelectedItem.Text); // is enough

//this code also throws compiler error, 
result =Convert.ToDateTime(datenow).ToString() - Convert.ToInt32(age).ToString();
 
Try this
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;
calculate-the-difference-between-two-dates-and-get-the-value-in-years[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Your code is rather weird.
You seem to have little understanding of the C# type system[^].
 
You seem to try to calculate a start date/time by subtract some "age" from "now".
What is "age"? seconds, minutes, hours, days, weeks, monts, years, centuries, mayan-cycles Wink | ;-) ...?
 
Anyways: when calculating with times, you need to understand the difference between
- a point in time (like "now", "yesterday", "last year", etc.)
- and a duration (like "1 hour", "20 years", etc.).
 
Durations can be added/subtracted from each other, resulting in a new duration.
Adding/subtracting two points in dime result in a durations.
Adding/subtracting a duration to/from a point in time gives a new point in time.
 
DateTime denotes a point in time.
TimeSpan denotes a duration.
 
In your code, I assume "age" to be a duration.
1) set the point in time variable "now" to DateTime.Now
2) parse the "duration" input text into a TimeSpan (you need to decide on the right unit)
3) subtract the "duration" from "now"
4) convert the result to the desired point in time format (e.g. year only) and assign that to your text output variable
 
Cheers
Andi
 
PS: Strongly recommend to use TryParse(...) instead of any Convert.XXX(...) or Parse(...) methods. Reason: user input is potentially broken.
  Permalink  

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

  Print Answers RSS
0 CPallini 355
1 BillWoodruff 334
2 George Jonsson 279
3 Sergey Alexandrovich Kryukov 243
4 Gihan Liyanage 225
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100