views:

433

answers:

4

Hi all.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 date. I want to get date of day name. I also used

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

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

+2  A: 

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) 
SLaks
but this Monday is a string .I am getting it from data base using reader["Day_Name"].It changes
Meko
Beat me to it :) +1
gmcalab
@Meko: `(DayOfWeek)Enum.Parse(typeof(DayOfWeek), reader["Day_Name"])`.
SLaks
@SLaks: The first code is example or I must use this function too?
Meko
The `Enum.Parse` call can be passed to `Next`.
SLaks
DateTime.Parse("08:15").Next(DaayOfWeek.Monday) here it does not call Next. It sees DateTime from System.Lib .I also added Next() function.
Meko
Did you add my extension method?
SLaks
Yes.I added that also
Meko
What error do you get?
SLaks
I made it .now works.thanks but I have a little problem now.In my table day names like Monday,Tuesday,Friday,Monday,Wednesday.. If today Tuesday that it will get first Monday from list and sets that it is next week but it must get Friday.How can I do it?
Meko
A: 

You can use the ctor 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

kjkerstens
but I have no specific date like DateTime someTimeNextWeek = new DateTime(2010, 05, 10, 08, 15, 00); Program will run on server like an 1 year and every time I must change this value?It will be complex I think.
Meko
This does not answer the question.
SLaks
A: 

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 dont know.I made some checking but I dont sure that it will work )).If some body find some thing stupid in code please mention to me :)) And @SLaks your code works well ) thanks.

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


        }
Meko
A: 

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