在C#中将两个整数解析为datetime时遇到问题。我有两个整数,如下所示:
int day = 25;
int month = 08;
var currentYear = DateTime.Now.Year;
var scheduledDate = "25/08/2018";
我想要的是将day, month and the current year
转换为格式dd/MM/yyyy
,然后将此日期与格式为dd/MM/yyyy
的scheduleDate进行比较。有人可以帮我吗?
答案 0 :(得分:4)
您应该始终将let
myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"]
i = 1;
while i <= length(myfriends) # this function here seems to work no problem
friend = myfriends[i]
println("Hi $friend, it's great to see you!")
i = i + 1 # gives syntax error when prefixed with global
end
end
转换为DateTime
对象,然后进行比较,而不是将String
对象转换为String
。
DateTime
答案 1 :(得分:2)
var yourDateTime = new DateTime(currentYear, month, day);
var yourDateString = yourDateTime.ToString("dd/MM/yyyy");
// compare
答案 2 :(得分:2)
答案 3 :(得分:1)
尝试以下操作,您可以使用DateTime.ToString()方法将其转换为所需的format。
int day = 25;
int month = 08;
var currentYear = DateTime.Now.Year;
var date = new DateTime(DateTime.Now.Year, month, day);
var formattedDate = date.ToString("dd/MM/yyyy");
var scheduledDate = "25/08/2018";
// Compare formattedDate and scheduledDate
OR
如果更合适,您可以考虑将scheduleDate字符串转换为日期时间并比较日期时间对象:
var scheduledDate = "25/08/2018";
var parsedDate = DateTime.ParseExact(scheduledDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
var date = new DateTime(DateTime.Now.Year, month, day);
// compare date & parsed date
bool areEqual = date.Equals(parsedDate);
答案 4 :(得分:1)
如果我理解您的问题,只需将进行中的操作保持为零,
int day = 25;
int month = 08;
var currentYear = DateTime.Now.Year;
var targetDate = $"{day}/{month.ToString().PadLeft(2,'0')}/{currentYear}";
var scheduledDate = "25/08/2018";
var difference = DateTime.Compare(DateTime.Parse(targetDate),
DateTime.Parse(scheduledDate));
答案 5 :(得分:1)
如果只想将其转换为字符串,这应该是最简单的方法:
int day = 25;
int month = 08;
var currentYear = DateTime.Now.Year;
// {day:D2} is equivalent to day.ToString("D2") which pads integers to a length of two
var createdDate = $"{day:D2}/{month:D2}/{currentYear}";
var scheduledDate = "25/08/2018";
编辑
整理一下人们在这里所说的一些内容,将它们都解析为DateTime
可能更适合于比较部分:
DateTime createdDateTime = new DateTime(currentYear, month, day);
DateTime scheduledDateTime = DateTime.ParseExact(scheduledDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
// Compare if it's the same day
if (createdDateTime.Date == scheduledDateTime.Date)
{
// do stuff
}
// Get the difference in Days
int dayDifference = (createdDateTime - scheduledDateTime).Days;
或者用.TotalDays
代替.Days
返回double
答案 6 :(得分:0)
int day = 25;
int month = 08;
var currentYear = DateTime.Now.Year;
var scheduledDate = "25/08/2018";
// parse string into DateTime structure
var ci = new CultureInfo("en-UK");
var dtScheduledDate = DateTime.Parse(scheduledDate, ci);
var dtOther = new DateTime(currentYear, month, day);
if(dtOther > dtScheduledDate)
{
...
}
答案 7 :(得分:0)
在这种情况下,如果变量scheduleDate
是字符串,则可以将其他变量(day,month and currentYear)
转换为所需格式的字符串:
var dt = day + "/" + month + "/" + currentYear;
因此,您可以这样做:
if(dt.Equals(scheduleDate))
//Do some
else
//other thing
答案 8 :(得分:0)
static void Main(string[] args)
{
int transactionDate = 20201010;
int? transactionTime = 210000;
var agreementDate = DateTime.Today;
var previousDate = agreementDate.AddDays(-1);
var agreementHour = 22;
var agreementMinute = 0;
var agreementSecond = 0;
var startDate = new DateTime(previousDate.Year, previousDate.Month, previousDate.Day, agreementHour, agreementMinute, agreementSecond);
var endDate = new DateTime(agreementDate.Year, agreementDate.Month, agreementDate.Day, agreementHour, agreementMinute, agreementSecond);
DateTime selectedDate = Convert.ToDateTime(transactionDate.ToString().Substring(6, 2) + "/" + transactionDate.ToString().Substring(4, 2) + "/" + transactionDate.ToString().Substring(0, 4) + " " + string.Format("{0:00:00:00}", transactionTime));
Console.WriteLine("Selected Date : " + selectedDate.ToString());
Console.WriteLine("Start Date : " + startDate.ToString());
Console.WriteLine("End Date : " + endDate.ToString());
if (selectedDate > startDate && selectedDate <= endDate)
Console.WriteLine("Between two dates..");
else if (selectedDate <= startDate)
Console.WriteLine("Less than or equal to the start date!");
else if (selectedDate > endDate)
Console.WriteLine("Greater than end date!");
else
Console.WriteLine("Out of date ranges!");
}
输出: 选择日期:10.10.2020 21:00:00 开始日期:8.10.2020 22:00:00 结束日期:9.10.2020 22:00:00 大于结束日期!