views:

208

answers:

7

in dot net frame work 2

how to better this and easy to used i have many times

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string timenow = DateTime.Now.ToString("T");
        Label1.Text = DateTime.Now.ToString("T");
        Label3.Text = DateTime.Now.ToString("D");
        Label2.Text = getTimeZone(timenow);
    }

    //hh:mm:ss
    protected string getTimeZone(string time)
    {
        string[] e = time.Split(':');
        int h = Convert.ToInt16(e[0]);
        int m = Convert.ToInt16(e[1]);
        int s = Convert.ToInt16(e[2]);

        if ((h == 15) && (m >= 1) && (m <= 30) && (s <= 59))
        {
            return "15.01 - 15.30";
        }
        if ((h == 15) && (m >= 31) && (m <= 45) && (s <= 59))
        {
            return "15.31 - 15.45";
        }
        if ((h == 15) && (m >= 46) && (m == 0) && (s <= 59))
        {
            return "15.31 - 15.45";
        }

        return time;
    }
}
+1  A: 

Why you are using string instead of DateTime directly, you can use DateTime.TimeofDay

Ahmed Said
A: 

Well, I'd take getTimeZone out of the page class and put it in a util class for a start...

UpTheCreek
Get it to work first, and then consider to make it available in other scenarios by exposing it in a utiliy class.
awe
Well, it sounds like it is working already, he just wants a bettwe way of doing it. He says he has used it many times, so I assume this code is all over the place at the moment. Putting it in util class will help him when he's refactoring.
UpTheCreek
+9  A: 
DateTime now = DateTime.Now;
DateTime start = new DateTime(now.Year, now.Month, now.Day, 15, 31, 0);
DateTime end = new DateTime(now.Year, now.Month, now.Day, 15, 45, 0);

if (now >= start && now <= end)
    Console.WriteLine("We have a go!");
Matthew Scharley
+1 you can directly compare more than numbers.
Tadeusz A. Kadłubowski
If you want the pattern for the minutes span to apply for all hours in a day, this will result in a lot of if statements...
awe
Not really, you just get into TimeSpans and the like, or use your own solution. The point was more that you can compare DateTime's directly, and you *should*, not parse them to strings, then parse out the strings to integers and fiddle around like that.
Matthew Scharley
I agree that you should not go via string parsing to int and fiddle around. Your answer was making a good point about that. I provided an answer that takes out the different parts directly from the DateTime object, and checks against the minutes part of it. This way it works also for any hour in the day. Of course, this only works if the minute pattern is same for all hours...
awe
A: 

You could parse the strings to DateTime objects and compare them. You also you use string.CompareTo() to compare the strings directly.

codymanix
No need to even do that, since he's starting with DateTime objects and calling ToString on them...
Matthew Scharley
+1  A: 

What about something like this:

protected void Page_Load()
{
    DateTime timenow = DateTime.Now;
    Label2.Text = getTimeZone(timenow);
}

//hh:mm:ss
protected string getTimeZone(DateTime time)
{
    if (time.Hour == 15 && time.Minute >= 1 && time.Minute <= 30)
        return "15.01 - 15.30";
    if (time.Hour == 15 && time.Minute >= 31 && time.Minute <= 45)
        return "15.31 - 15.45";
    return "";
}
M4N
A: 
protected void Page_Load(object sender, EventArgs e)
{
    DateTime now = DateTime.Now;

    Label1.Text = now.ToString("T");
    Label3.Text = now.ToString("D");

    if (now.Hour == 15 && now.Minute >= 1 && now.Minute <= 30)
        Label2.Text = "15.01 - 15.30";
    else if (now.Hour == 15 && now.Minute >= 31 && now.Minute <= 45)
        Label2.Text = "15.31 - 15.45";
    else
        Label2.Text = now.ToString("T");
}
LukeH
+1  A: 

This would work if same pattern for all hours:

protected void Page_Load()
{
    DateTime timenow = DateTime.Now;
    Label1.Text = timenow.ToString("T");        
    Label3.Text = timenow.ToString("D");        
    Label2.Text = getTimeZone(timenow);         
}

//hh:mm:ss
protected string getTimeZone(DateTime time)
{
    if (time.Minute >= 1 && time.Minute <= 30)
        return String.Format("{0:00}.01 - {0:00}.30", time.Hour);
    if (time.Minute >= 31 && time.Minute <= 45)
        return String.Format("{0:00}.31 - {0:00}.45", time.Hour);
    if (time.Minute >= 46)
        return String.Format("{0:00}.46 - {1:00}.00", time.Hour, (time.Hour < 23) ?  time.Hour+1 : 0);
    if (time.Minute == 00)
        return String.Format("{0:00}.46 - {1:00}.00", (time.Hour > 0) ? time.Hour-1 : 23, time.Hour);
    return "";
}
awe