我如何将dataetime转换为int,将string转换为Int [英] How i convert dataetime to int and string to Int

查看:131
本文介绍了我如何将dataetime转换为int,将string转换为Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();

推荐答案

看起来你想要多年使用年龄DateTime.Now和出生日期。



这样就可以了。



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();





但由于问题的标题是我如何将dataetime转换为int并将字符串转换为Int这是以下





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();


代码存在严重问题它甚至不会编译。你完全混淆了转换。我建议也要了解赋值操作的基础知识。



你试图将字符串数据存储到以下位置的整数变量''age'',这是错误你永远不会将字符串存储到int中。

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--;



计算 - 两个日期之间的差异 - 并且每年获得价值 [ ^ ]

你的代码很奇怪。

你似乎对 C#类型系统 [ ^ ]。



您似乎试图通过减去一些年龄来计算开始日期/时间从现在。

什么是年龄?秒,分钟,小时,天,周,monts,年,世纪,玛雅周期; - )......?



无论如何:在计算时间时,你需要了解

之间的区别 - 时间点(如现在,昨天,去年等)

- 和持续时间(如1小时,20年等)。



持续时间可以加/相互减去,产生一个新的持续时间。

在一个角钱中加上/减去两个点会产生持续时间。

在某个点上加/减一个持续时间时间给出了一个新的时间点。



DateTime 表示一个时间点。

TimeSpan 表示持续时间。



在您的代码中,我假设年龄为持续时间。

1)将时间点变量now设置为 DateTime.Now

2)解析持续时间 将文本输入 TimeSpan (您需要决定正确的单位)

3)从现在中减去持续时间

4)将结果转换为所需的时间点格式(例如仅限年份)并将其分配给您的文本输出变量



干杯

Andi



PS:强烈建议使用 TryParse(...)而不是任何 Convert.XXX(...) Parse(...)方法。原因:用户输入可能已损坏。
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 ;-)...?

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.


这篇关于我如何将dataetime转换为int,将string转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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