我需要使用 c# 从 2015 年减去一年(比方说 1991 年)
I need to deduct a year (lets say 1991) from 2015, using c#
几个小时以来,我一直在寻找解决这个非常简单的代码的方法。我昨天开始了我的编程生涯,今天得到了 Visual Studio。我曾尝试玩 "Hello World" 练习,但是从彼此中扣除两年变得比我想象的要困难。
我的代码确实显示了我目前的水平,我求求你们,我到底做错了什么?
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
// Keep the console window open in debug mode.
Console.Write("Please enter your first name: ");
string firstName = Console.ReadLine();
Console.Write("Please enter your last name: ");
string lastName = Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("You're Name is: " + firstName + " " + lastName);
Console.WriteLine("");
Console.WriteLine("Please enter your birthyear");
Console.WriteLine("");
string yob = Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Today it's...");
DateTime time = DateTime.Now; // Use current time.
string format = "yyyy"; // Use this format. (MMM ddd d HH:mm yyyy)
Console.WriteLine(time.ToString(format)); // Write to console.
Console.WriteLine(" ");
Console.WriteLine("Which means that you are approximately....");
Console.WriteLine("?? Years old");
//timespan between `datenow` and `date1`
Console.ReadKey();
}
}
}
System.TimeSpan 将帮助您计算差异。
快速搜索 "difference in dates c#" 得到了这段代码。如果您想了解更多信息,页面上有多种方法。如果您需要更多帮助,请告诉我。
using System;
using System.Collections.Generic;
using System.Text;
namespace Console_DateTime
{
class Program
{
static void Main(string[] args)
{
System.DateTime dtTodayNoon = new System.DateTime(2006, 9, 13, 12, 0, 0);
System.DateTime dtTodayMidnight = new System.DateTime(2006, 9, 13, 0, 0, 0);
System.TimeSpan diffResult = dtTodayNoon.Subtract(dtYestMidnight);
Console.WriteLine("Yesterday Midnight - Today Noon = " + diffResult.Days);
Console.WriteLine("Yesterday Midnight - Today Noon = " + diffResult.TotalDays);
Console.ReadLine();
}
}
}
编辑:忘记解释的第一行。
将yob
转换为整数:
int yearOfBirth = int.Parse(yob); //This could fail! See note below
然后从今天的年份中减去:
int yearsOld = DateTime.Now.Year - yearOfBirth;
Console.WriteLine(yearsOld + " years old");
注意:如果您输入的不是数字,例如 two thousand
,int.Parse
命令可能会失败。查看 int.TryParse
来处理这种情况。
// get current time
DateTime now = DateTime.Now;
// get year of birth from user
String myYear = Console.ReadLine();
// construct a DateTime from that year
DateTime my = new DateTime(Convert.ToInt16(myYear), 1, 1);
// devide by 365 and convert to int
Console.Write(Convert.ToInt16((now - my).TotalDays) / 365);
几个小时以来,我一直在寻找解决这个非常简单的代码的方法。我昨天开始了我的编程生涯,今天得到了 Visual Studio。我曾尝试玩 "Hello World" 练习,但是从彼此中扣除两年变得比我想象的要困难。
我的代码确实显示了我目前的水平,我求求你们,我到底做错了什么?
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
// Keep the console window open in debug mode.
Console.Write("Please enter your first name: ");
string firstName = Console.ReadLine();
Console.Write("Please enter your last name: ");
string lastName = Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("You're Name is: " + firstName + " " + lastName);
Console.WriteLine("");
Console.WriteLine("Please enter your birthyear");
Console.WriteLine("");
string yob = Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Today it's...");
DateTime time = DateTime.Now; // Use current time.
string format = "yyyy"; // Use this format. (MMM ddd d HH:mm yyyy)
Console.WriteLine(time.ToString(format)); // Write to console.
Console.WriteLine(" ");
Console.WriteLine("Which means that you are approximately....");
Console.WriteLine("?? Years old");
//timespan between `datenow` and `date1`
Console.ReadKey();
}
}
}
System.TimeSpan 将帮助您计算差异。 快速搜索 "difference in dates c#" 得到了这段代码。如果您想了解更多信息,页面上有多种方法。如果您需要更多帮助,请告诉我。
using System;
using System.Collections.Generic;
using System.Text;
namespace Console_DateTime
{
class Program
{
static void Main(string[] args)
{
System.DateTime dtTodayNoon = new System.DateTime(2006, 9, 13, 12, 0, 0);
System.DateTime dtTodayMidnight = new System.DateTime(2006, 9, 13, 0, 0, 0);
System.TimeSpan diffResult = dtTodayNoon.Subtract(dtYestMidnight);
Console.WriteLine("Yesterday Midnight - Today Noon = " + diffResult.Days);
Console.WriteLine("Yesterday Midnight - Today Noon = " + diffResult.TotalDays);
Console.ReadLine();
}
}
}
编辑:忘记解释的第一行。
将yob
转换为整数:
int yearOfBirth = int.Parse(yob); //This could fail! See note below
然后从今天的年份中减去:
int yearsOld = DateTime.Now.Year - yearOfBirth;
Console.WriteLine(yearsOld + " years old");
注意:如果您输入的不是数字,例如 two thousand
,int.Parse
命令可能会失败。查看 int.TryParse
来处理这种情况。
// get current time
DateTime now = DateTime.Now;
// get year of birth from user
String myYear = Console.ReadLine();
// construct a DateTime from that year
DateTime my = new DateTime(Convert.ToInt16(myYear), 1, 1);
// devide by 365 and convert to int
Console.Write(Convert.ToInt16((now - my).TotalDays) / 365);