C# 格式化年龄 - 关于天、周、月 - 年
我正在开发一些医疗软件,我需要根据以下规则以非常具体的方式输出所有年龄:
If under 6 Weeks old : ###D (Number of Days)
If under 6 Months old : ###W (Number of Weeks)
If under 2 Years old : ###M (Number of Months)
If above 2 Years old : ###Y (Number of Years)
使用 C# 我试图找到一种简单的方法来做到这一点,只需使用一个人的出生日期,任何帮助将不胜感激。
I am working on some medical software and I am required to output all ages in a very specific manner, based on the following rules:
If under 6 Weeks old : ###D (Number of Days)
If under 6 Months old : ###W (Number of Weeks)
If under 2 Years old : ###M (Number of Months)
If above 2 Years old : ###Y (Number of Years)
Using C# I am trying to find a simple method of doing this just using a Person's Date of Birth, any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我昨天正在研究类似的东西,但是这样的东西应该适合您的需求:(假设每周 7 天,每月 31 天,每年 365 天等。)
修订方法: (根据鲍勃的建议修复)
以前的方法:
希望这有帮助!
I was working on something similar yesterday, but something like this should suit your needs: (assuming 7 day weeks, 31 day months, 365 day years etc.)
Revised Method : (Fixed as per Bob's suggestions)
Previous Method :
Hope this helps!
可以从其他 DateTime 中减去 DateTime 类型,从而得到表示间隙的 TimeSpan。试试这个:
然后,查看 timeAlive 的日、月和年(将日除以 7 得到周),并相应地设置格式。
A DateTime type can be subtracted from other DateTimes, resulting in a TimeSpan representing the gap. Try this:
Then, look at the Days, Months and Years (divide Days by 7 for Weeks) of timeAlive, and format accordingly.
以下内容不对天/月或年做出任何假设。
缺点是它不兼容 Y3K。
The following makes no assumptions about days/months or year.
On the downside, it is not Y3K compatible.
您可以通过简单的减法获得代表用户当前年龄的对象:
然后只需执行一堆 if 子句即可
您应该能够弄清楚如何进行格式化。
You can get an object representing the user's current age with a simple subtraction:
And then it's just a matter of doing a bunch of if clauses
You should be able to figure out how to do your formatting.