DateTime Control in ASP.NET EST vs PST ???

Hi There,

I have a datetime control that I am using and am having some issues with the hour portion.  Depending on format the user wants the control will display a drop down with portions of the date and time.  Here is the code that generates the hour dropdown:

hourPart = new DateDropDownList(DateTimePart.Hour);
int HrWork = 0;
for(i=9;i<=18;i++)
{
HrWork = i;
if (i > 12)
    HrWork = i - 12;
    hourPart.Items.Add( new ListItem(HrWork.ToString("d2"), HrWork.ToString()) );
}
Controls.Add(hourPart);

Since this user only is available from 9 to 6 the dropdown only displays those hours.  The issue is at the other end.  When they pick an hour from the dropdown, for example, 10 the control is returning 1.  It's as though it is assuming PST for the control and EST when I retreive the data.  Here is the code that retrieves the data:

StartHourEmail = StartHour.SelectedDateTime.Hour.ToString("00");

There is probably an easy answer for you experts.  This is kind of urgent.
Does anyone have any ideas why this is happening??

Thanks,
David

skinsfan99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jens FiedererTest Developer/ValidatorCommented:
Are you sure you are not getting the Selected INDEX in your control (starting at 9, the zero-based index of 10 would be 1).
0
Jens FiedererTest Developer/ValidatorCommented:
If that is not it, you'll have to post more code of how your control works - there is really no way of telling what
SelectedDateTime
does without seeing code for the control.
0
skinsfan99Author Commented:
Here is the code that builds the control:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Globalization;
using System.Text;

namespace CI_CMS_Controls.Web.Controls
{
      /// <summary>
      /// Date/time picker
      /// </summary>
      [ToolboxData("<{0}:DateTimePicker runat=server></{0}:DateTimePicker>")]
      [Designer(typeof(CI_CMS_Controls.Web.Controls.Designers.DateTimePickerDesigner))]
      public class DateTimePicker : System.Web.UI.WebControls.WebControl, INamingContainer
      {
            CheckBox allowNullCheckBox;

            #region Private methods
            private void AddLiteral(String text)
            {
                  Literal literal = new Literal();
                  literal.Text = text;
                  Controls.Add(literal);
            }

            private void ParseDateFormat(String dateFormat)
            {
                  Char lastChar = ' ';
                  Boolean firstTime = true;
                  StringBuilder sequence = new StringBuilder();
                  foreach (Char c in dateFormat)
                  {
                        if (firstTime)
                              firstTime = false;
                        else
                        {
                              if (c != lastChar)
                              {
                                    AddControlForText(sequence.ToString());
                                    sequence.Length = 0;
                              }
                        }
                        lastChar = c;
                        sequence.Append(c);
                  }
                  if (sequence.Length > 0)
                        AddControlForText(sequence.ToString());
            }
            #endregion

            #region Protected methods
            protected virtual void AddControlForText(String text)
            {
                  Int32 i;
                  DateTimeFormatInfo dateTimeFormatInfo = new DateTimeFormatInfo();
                  DateDropDownList datePart;
                  DateDropDownList monthPart;
                  TextBox yearPart;
                  DateDropDownList hourPart;
                  DateDropDownList minutePart;
                  DateDropDownList secondPart;
                  switch(text)
                  {
                        case "D":
                              ParseDateFormat(dateTimeFormatInfo.LongDatePattern);
                              break;

                        case "T":
                              ParseDateFormat(dateTimeFormatInfo.LongTimePattern);
                              break;

                        case "yy":
                              yearPart = new TextBox();
                              yearPart.MaxLength = 2;
                              yearPart.Columns = 2;
                              Controls.Add(yearPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "yyyy":
                              yearPart = new TextBox();
                              yearPart.MaxLength = 4;
                              yearPart.Columns = 4;
                              Controls.Add(yearPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "M":
                              monthPart = new DateDropDownList(DateTimePart.Month);
                              for (i = 1; i <= 12; i++)
                                    monthPart.Items.Add( new ListItem(i.ToString(), i.ToString()) );
                              Controls.Add(monthPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "MM":
                              monthPart = new DateDropDownList(DateTimePart.Month);
                              for (i = 1; i <= 12; i++)
                                    monthPart.Items.Add( new ListItem(i.ToString("d2"), i.ToString()) );
                              Controls.Add(monthPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "MMM":
                              monthPart = new DateDropDownList(DateTimePart.Month);
                              for (i = 1; i <= 12; i++)
                                    monthPart.Items.Add( new ListItem(dateTimeFormatInfo.MonthNames[i-1].Substring(0, 3), i.ToString()) );
                              Controls.Add(monthPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "MMMM":
                              monthPart = new DateDropDownList(DateTimePart.Month);
                              for (i = 1; i <= 12; i++)
                                    monthPart.Items.Add( new ListItem(dateTimeFormatInfo.MonthNames[i-1], i.ToString()) );
                              Controls.Add(monthPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "d":
                              datePart = new DateDropDownList(DateTimePart.Date);
                              for (i = 1; i <= 31; i++)
                                    datePart.Items.Add( new ListItem(i.ToString(), i.ToString()) );
                              Controls.Add(datePart);
                              AddLiteral("&nbsp;");
                              break;

                        case "dd":
                              datePart = new DateDropDownList(DateTimePart.Date);
                              for (i = 1; i <= 31; i++)
                                    datePart.Items.Add( new ListItem(i.ToString("d2"), i.ToString()) );
                              Controls.Add(datePart);
                              AddLiteral("&nbsp;");
                              break;

                        case "h":
                        case "H":
                              hourPart = new DateDropDownList(DateTimePart.Hour);
                              int HrWork = 0;
                              for(i=9;i<=18;i++)
                              {
                                    HrWork = i;
                                    if (i > 12)
                                          HrWork = i - 12;

                                    hourPart.Items.Add( new ListItem(HrWork.ToString("d2"), HrWork.ToString()) );
                              }
                              Controls.Add(hourPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "hh":
                        case "HH":
                              hourPart = new DateDropDownList(DateTimePart.Hour);
                              for (i = 0; i <= 23; i++)
                                    hourPart.Items.Add( new ListItem(i.ToString("d2"), i.ToString()) );
                              Controls.Add(hourPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "m":
                              minutePart = new DateDropDownList(DateTimePart.Minute);
                              for (i = 0; i <= 59; i++)
                                    minutePart.Items.Add( new ListItem(i.ToString(), i.ToString()) );
                              Controls.Add(minutePart);
                              AddLiteral("&nbsp;");
                              break;

                        case "mm":
                              minutePart = new DateDropDownList(DateTimePart.Minute);
                              for (i = 0; i <= 59; i++)
                                    minutePart.Items.Add( new ListItem(i.ToString("d2"), i.ToString()) );
                              Controls.Add(minutePart);
                              AddLiteral("&nbsp;");
                              break;

                        case "s":
                              secondPart = new DateDropDownList(DateTimePart.Second);
                              for (i = 0; i <= 59; i++)
                                    secondPart.Items.Add( new ListItem(i.ToString(), i.ToString()) );
                              Controls.Add(secondPart);
                              AddLiteral("&nbsp;");
                              break;

                        case "ss":
                              secondPart = new DateDropDownList(DateTimePart.Second);
                              for (i = 0; i <= 59; i++)
                                    secondPart.Items.Add( new ListItem(i.ToString("d2"), i.ToString()) );
                              Controls.Add(secondPart);
                              AddLiteral("&nbsp;");
                              break;

                        default:
                              break;
                  }
            }

            protected override void CreateChildControls()
            {
                  base.CreateChildControls ();
                  if (AllowNull)
                  {
                        Controls.Add(allowNullCheckBox);
                        allowNullCheckBox.Visible = false;
                  }
                  ParseDateFormat(DateFormat);
            }//CreateChildControls
            #endregion

            #region Public methods
            public new void EnsureChildControls()
            {
                  base.EnsureChildControls();
            }

            #endregion

            #region Public properties

            [DefaultValue("D")]
            public String DateFormat
            {
                  get {return (string)ViewState["DateFormat"];}
                  set {
                        ViewState["DateFormat"] = value;
                        ChildControlsCreated = false;
                  }
            }

            [Browsable(false)]
            [Bindable(true)]
            public DateTime SelectedDateTime
            {
                  get
                  {
                        Int32 datePart = Int32.MaxValue;
                        Int32 monthPart = Int32.MaxValue;
                        Int32 yearPart = Int32.MaxValue;
                        Int32 hourPart = 0;
                        Int32 minutePart = 0;
                        Int32 secondPart = 0;
                        Boolean timeOnly = true;

                        foreach (Control control in Controls)
                        {
                              if (control is TextBox)
                              {
                                    yearPart = Int32.Parse( (control as TextBox).Text );
                                    timeOnly = false;
                              }
                              else
                              {
                                    if (control is DateDropDownList)
                                    {
                                          switch( (control as DateDropDownList).DateTimePart )
                                          {
                                                case DateTimePart.Second:
                                                      secondPart = (control as DateDropDownList).SelectedIndex;
                                                      break;

                                                case DateTimePart.Minute:
                                                      minutePart = (control as DateDropDownList).SelectedIndex;
                                                      break;

                                                case DateTimePart.Hour:
                                                      hourPart = (control as DateDropDownList).SelectedIndex;
                                                      break;

                                                case DateTimePart.Date:
                                                      datePart = (control as DateDropDownList).SelectedIndex + 1;
                                                      timeOnly = false;
                                                      break;

                                                case DateTimePart.Month:
                                                      monthPart = (control as DateDropDownList).SelectedIndex + 1;
                                                      timeOnly = false;
                                                      break;

                                                case DateTimePart.Year:
                                                      yearPart = Int32.Parse((control as DateDropDownList).SelectedValue);
                                                      timeOnly = false;
                                                      break;
                                          }//switch
                                    }//control is DateDropDownList
                              }//else
                        }//foreach
                        if (timeOnly)
                        {
                              yearPart = DateTime.Today.Year;
                              monthPart = DateTime.Today.Month;
                              datePart = DateTime.Today.Day;
                        }
                        return new DateTime(yearPart, monthPart, datePart, hourPart, minutePart, secondPart);
                  }//get

                  set
                  {
                        EnsureChildControls();
                        foreach(Control control in Controls)
                        {
                              if (control is TextBox)
                                    (control as TextBox).Text = value.Year.ToString("d" + (control as TextBox).MaxLength);
                              else
                              {
                                    if (control is DateDropDownList)
                                    {
                                          DateDropDownList dateDropDownList = (DateDropDownList) control;
                                          switch (dateDropDownList.DateTimePart)
                                          {
                                                case DateTimePart.Year:
                                                      Int32 dateLength = dateDropDownList.Items[0].Value.Length;
                                                      String year = value.Year.ToString("d" + dateLength.ToString());
                                                      Int32 itemIndex = dateDropDownList.Items.IndexOf(new ListItem(year));
                                                      if (itemIndex == -1)
                                                            throw new Exception("Year is not within the require range");
                                                      dateDropDownList.SelectedIndex = itemIndex;
                                                      break;

                                                case DateTimePart.Month:
                                                      dateDropDownList.SelectedIndex = value.Month - 1;
                                                      break;

                                                case DateTimePart.Date:
                                                      dateDropDownList.SelectedIndex = value.Day - 1;
                                                      break;

                                                case DateTimePart.Hour:
                                                      dateDropDownList.SelectedIndex = value.Hour;
                                                      break;

                                                case DateTimePart.Minute:
                                                      dateDropDownList.SelectedIndex = value.Minute;
                                                      break;

                                                case DateTimePart.Second:
                                                      dateDropDownList.SelectedIndex = value.Second;
                                                      break;

                                          }//switch
                                    }//control is DateDropDownList
                              }//else
                        }//foreach
                  }//set
            }

            [Browsable(false)]
            public Boolean IsValid
            {
                  get
                  {
                        try
                        {
                              DateTime test = this.SelectedDateTime;
                              return true;
                        }
                        catch (Exception)
                        {
                              return false;
                        }
                  }
            }//DateTimeIsValid

            [DefaultValue(true)]
            public Boolean AllowNull
            {
                  get
                  {return (Boolean)ViewState["AllowNull"];}
                  set
                  {
                        ViewState["AllowNull"] = value;
                        if (!value)
                        {
                              if (!IsValid)
                                    SelectedDateTime = DateTime.Today;
                              IsNull = false;
                        }
                        ChildControlsCreated = false;
                  }
            }//AllowNull

            [Browsable(false)]
            public Boolean IsNull
            {
                  get
                  {
                        EnsureChildControls();
                        return !allowNullCheckBox.Checked;
                  }
                  set
                  {
                        EnsureChildControls();
                        allowNullCheckBox.Checked = !value;
                  }
            }//IsNull

            #endregion

            #region Constructors
            public DateTimePicker() : base()
            {
                  allowNullCheckBox = new CheckBox();
                  DateFormat = "D";
                  AllowNull = true;
                  IsNull = false;
                  SelectedDateTime = DateTime.Today;
            }
            #endregion

      }//Class
}

 This control was pulled from a ASP.NET code sharing site.  I am somewhat new to C# and don't fulling understand all of the code.   What you are saying about the index makes sense but I don't see a value, text or selected index property.   If you need more let me know.

Thanks
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jens FiedererTest Developer/ValidatorCommented:
Take a look at the
            public DateTime SelectedDateTime
method, which is what you are using.

Inside that, there is THIS code:
                                                case DateTimePart.Hour:
                                                      hourPart = (control as DateDropDownList).SelectedIndex;
                                                      break;
Notice the hourPart is set to the SelectedIndex ... the code takes advantage of the "fact" that the hours are equal to their index in the dropdownlist, which is no longer true.

Assuming the DateDropDownList works like a regular DropDownList, you should be able to use
hourPart = Integer.Parse((control as DateDropDownList).Text)
instead.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skinsfan99Author Commented:
I actually had to do this:
                                          
hourPart = Int32.Parse((control as DateDropDownList).SelectedValue);

but you were right on the money.  THANK YOU!!!!!
0
Jens FiedererTest Developer/ValidatorCommented:
Thanks, my pleasure!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.