我正在尝试从日期名称和时间获取日期格式,如“星期一”和第二个字符串“08:15”,它必须像10:05:2010 08:15
,然后我将从今天的日期减去。
DateTime.Parse("08:15")
它有效,但它输出了今天的日期。我想获得日期名称的日期。我也用过
DateTime.Parse("08:15").AddDays(1)
它给了我下一天的约会。在这里,我希望获得下周一的日期和“08:15”时间。
答案 0 :(得分:3)
要获得下周一的日期,您可以使用以下扩展方法:
///<summary>Gets the first week day following a date.</summary>
///<param name="date">The date.</param>
///<param name="dayOfWeek">The day of week to return.</param>
///<returns>The first dayOfWeek day following date, or date if it is on dayOfWeek.</returns>
public static DateTime Next(this DateTime date, DayOfWeek dayOfWeek) {
return date.AddDays((dayOfWeek < date.DayOfWeek ? 7 : 0) + dayOfWeek - date.DayOfWeek);
}
你可以像这样使用它:
DateTime.Parse("08:15").Next(DaayOfWeek.Monday)
答案 1 :(得分:0)
您可以使用构造函数随时生成。
DateTime someTimeNextWeek = new DateTime(2010, 05, 10, 08, 15, 00);
然后使用.ToString()
说明符获取所需的输出格式。
someTimeNextWeek .ToString("d") = 12/01/2004
someTimeNextWeek .ToString("D") = January 12, 2004
someTimeNextWeek .ToString("f") = January 12, 2004 10:02 PM
someTimeNextWeek .ToString("F") = January 12, 2004 10:02:10 PM
someTimeNextWeek .ToString("g") = 12/01/2004 10:02 PM
someTimeNextWeek .ToString("G") = 12/01/2004 10:02:10 PM
someTimeNextWeek .ToString("m") = January 12
someTimeNextWeek .ToString("r") = Mon, 12 Jan 2004 22:02:10 GMT
someTimeNextWeek .ToString("s") = 2004-01-12T22:02:10
someTimeNextWeek .ToString("t") = 10:02 PM
someTimeNextWeek .ToString("T") = 10:02:10 PM
someTimeNextWeek .ToString("u") = 2004-01-12 22:02:10Z
someTimeNextWeek .ToString("U") = January 13, 2004 6:02:10 AM
someTimeNextWeek .ToString("y") = January, 2004
答案 2 :(得分:0)
我希望在下周一和“08:15”时间获得约会。
它必须像10:05:2010 08:15
脱离我的头顶......
/// Assumes time is formatted as 08:15 and not 08:15:00
public static string FunnyEncode(string day, string time) {
DateTime dt = DateTime.Parse(
DateTime.Today.ToString("MM/dd/yyyy") + " " + time + ":00");
while (dt.DayOfWeek != day) // i.e. "Monday"
dt = dt.AddDays(1);
return dt.ToString("MM:dd:yyyy HH:mm");
}
答案 3 :(得分:-1)
我认为它是这样工作的。但我认为有最简单和更好的解决方案。而且我认为有一天会有一些错误,比如今天星期五和本周没有课程和下周的课程开始星期二。怎么会明白它是下周二而不是以前我不知道。我做了一些检查,但我不确定它会起作用))。如果某些机构在代码中发现一些愚蠢的东西,请向我提及:))。
private void timer1_Tick(object sender, EventArgs e)
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("select Lesson_Time from schedule where Lesson_Time >= (?LessonTime) AND Room_NO=(?RoomNO)And Day_Name = (?DayName) order by Lesson_Time ASC limit 0, 1 ", connection);
MySqlParameter param1 = new MySqlParameter();
MySqlParameter param2 = new MySqlParameter();
MySqlParameter param3 = new MySqlParameter();
param1.ParameterName = "?LessonTime";
param1.Value = DateTime.Now.AddMilliseconds(60000).ToShortTimeString();
param2.ParameterName = "?RoomNO";
param2.Value = serverListBox.SelectedItem.ToString();
param3.ParameterName = "?DayName";
param3.Value = DateTime.Today.Day;
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
cmd.Parameters.Add(param3);
cmd.ExecuteNonQuery();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
nextLessonTime = DateTime.Parse(reader["Lesson_Time"].ToString());
// timer1.Interval = ().;
Console.WriteLine(("Between Time ") + (nextLessonTime - DateTime.Now));
}
}
else
{
reader.Close();
MySqlCommand cmd2 = new MySqlCommand("select Day_Name , Lesson_Time from schedule where Room_NO=(?RoomNO)AND Week_NO=(?WeekNO)AND Day_Name=(?DayName) ", connection);
MySqlParameter param4 = new MySqlParameter();
MySqlParameter param5 = new MySqlParameter();
MySqlParameter param6 = new MySqlParameter();
param4.ParameterName = "?RoomNO";
param4.Value = serverListBox.SelectedItem.ToString();
param5.ParameterName = "?DayName";
param5.Value = DateTime.Today;
param6.ParameterName = "?WeekNO";
if (DateTime.Now.DayOfYear / 7 % 2 == 1)
param6.Value = (1);
else
param6.Value = (2);
cmd2.Parameters.Add(param4);
cmd2.Parameters.Add(param5);
cmd2.Parameters.Add(param6);
cmd2.ExecuteNonQuery();
reader2 = cmd2.ExecuteReader();
if (reader2.HasRows)
{
while (reader2.Read())
{
if (reader2["Day_Name"].ToString() == "Monday" && ((int)DayOfWeek.Monday - (int)DateTime.Today.DayOfWeek) > 0)
{
nextLessonTime = DateTime.Parse(reader2["Lesson_Time"].ToString()).AddDays((int)DayOfWeek.Monday - (int)DateTime.Today.DayOfWeek);
break;
}
else if (reader2["Day_Name"].ToString() == "Tuesday" && ((int)DayOfWeek.Tuesday - (int)DateTime.Today.DayOfWeek) > 0)
{
nextLessonTime = DateTime.Parse(reader2["Lesson_Time"].ToString()).AddDays((int)DayOfWeek.Tuesday - (int)DateTime.Today.DayOfWeek);
break;
}
else if (reader2["Day_Name"].ToString() == "Wednesday" && ((int)DayOfWeek.Wednesday - (int)DateTime.Today.DayOfWeek) > 0)
{
nextLessonTime = DateTime.Parse(reader2["Lesson_Time"].ToString()).AddDays((int)DayOfWeek.Wednesday - (int)DateTime.Today.DayOfWeek);
break;
}
else if (reader2["Day_Name"].ToString() == "Thursday" && ((int)DayOfWeek.Thursday - (int)DateTime.Today.DayOfWeek) > 0)
{
nextLessonTime = DateTime.Parse(reader2["Lesson_Time"].ToString()).AddDays((int)DayOfWeek.Thursday - (int)DateTime.Today.DayOfWeek);
break;
}
else if (reader2["Day_Name"].ToString() == "Friday" && ((int)DayOfWeek.Friday - (int)DateTime.Today.DayOfWeek) > 0)
{
nextLessonTime = DateTime.Parse(reader2["Lesson_Time"].ToString()).AddDays((int)DayOfWeek.Friday - (int)DateTime.Today.DayOfWeek);
break;
}
else if (reader2["Day_Name"].ToString() == "Saturday" && ((int)DayOfWeek.Saturday - (int)DateTime.Today.DayOfWeek) > 0)
{
nextLessonTime = DateTime.Parse(reader2["Lesson_Time"].ToString()).AddDays((int)DayOfWeek.Saturday - (int)DateTime.Today.DayOfWeek);
break;
}
else if (reader2["Day_Name"].ToString() == "Sunday" && ((int)DayOfWeek.Sunday - (int)DateTime.Today.DayOfWeek) > 0)
{
nextLessonTime = DateTime.Parse(reader2["Lesson_Time"].ToString()).AddDays((int)DayOfWeek.Sunday - (int)DateTime.Today.DayOfWeek);
break;
}
}
}
else
{
reader2.Close();
MySqlCommand cmd3 = new MySqlCommand("select Day_Name , Lesson_Time from schedule where Room_NO=(?RoomNO)AND Week_NO=(?WeekNO) limit 0,1 ", connection);
MySqlParameter param7 = new MySqlParameter();
MySqlParameter param8 = new MySqlParameter();
MySqlParameter param9 = new MySqlParameter();
param7.ParameterName = "?RoomNO";
param7.Value = serverListBox.SelectedItem.ToString();
param8.ParameterName = "?DayName";
param8.Value = DateTime.Today.Day;
param9.ParameterName = "?WeekNO";
if (DateTime.Now.DayOfYear / 7 % 2 == 1)
param9.Value = (2);
else
param9.Value = (1);
cmd3.Parameters.Add(param7);
cmd3.Parameters.Add(param8);
cmd3.Parameters.Add(param9);
cmd3.ExecuteNonQuery();
reader3 = cmd3.ExecuteReader();
if (reader3.HasRows)
{
while (reader3.Read())
{
nextLessonTime = DateTime.Parse(reader3["Lesson_Time"].ToString()).Next((DayOfWeek)Enum.Parse(typeof(DayOfWeek), reader3["Day_Name"].ToString()));
}
}
reader3.Close();
}
connection.Close();
nextInterval = (nextLessonTime - DateTime.Now).TotalMilliseconds.ToString();
Console.WriteLine("Next INTERVAL " + nextInterval);
timer1.Interval = (int)(nextLessonTime - DateTime.Now).TotalMilliseconds;
Console.WriteLine("Timer INTERVAL " + timer1.Interval);
this.logListView.Items.Add("Server " + serverListBox.SelectedItem.ToString() + " Searched at " + DateTime.Now + " Next Search at " + nextLessonTime);
}