开发者

Using DateTime.Parse with string

I am trying to get date format from Day name and time like "Monday" and second string "08:15" and it must like 10:05:2010 08:15 and then I will make subtracting from date of today.

DateTime.Parse("08:15") 

it works but it outputs today`s dat开发者_JAVA百科e. I want to get date of day name. I also used

DateTime.Parse("08:15").AddDays(1) 

it gives me next days date. Here I want to get date of next Monday with and "08:15" time.


To get next Monday's date, you can use the following extension method:

///<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); 
}

You can use it like this:

DateTime.Parse("08:15").Next(DaayOfWeek.Monday) 


I want to get date of next Monday with and "08:15" time.

it must like 10:05:2010 08:15

Off the top of my head...

/// 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");
}


You can use the constructor to produce any time you want.

DateTime someTimeNextWeek = new DateTime(2010, 05, 10, 08, 15, 00); 

And then use the .ToString() specifier to get the output format that you want.

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


I made it work like this .But I think there is easiest and more good solution .Also I think there will some bugs for some day like If today Friday and in this week there is no lesson and next week lesson starts Tuesday.how it will understand that it is next Tuesday not previous I don't know.I made some checking but I'm not sure that it will work )). If some body find some thing stupid in code please mention to me :)) .

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

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜