Solved

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

Posted on 2007-03-28
6
564 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:skinsfan99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18811159
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18811166
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
 

Author Comment

by:skinsfan99
ID: 18815291
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 18815405
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
 

Author Comment

by:skinsfan99
ID: 18815853
I actually had to do this:
                                          
hourPart = Int32.Parse((control as DateDropDownList).SelectedValue);

but you were right on the money.  THANK YOU!!!!!
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18816546
Thanks, my pleasure!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question