DateTime array element does not contain the correct value

datetime matlab
datenum matlab
convert datetime array to cell array matlab
comparison is not defined between datetime and double arrays
datetime to array
matlab duration
matlab table to datetime
matlab convert datetime to duration

I've created a simple DateTime array that contains 3 items. These items are set to use the values of three different DateTimePickers on my form. Before I go further into using the array, I need to make sure it is actually using the correct values, and it does not appear to be doing so. Here's my code:

namespace Test
{
    public partial class Form1 : Form
    {
        DateTime[] monSchedule = new DateTime[3];

        public Form1()
        {
            InitializeComponent();

            monSchedule[0] = monStart.Value;
            monSchedule[1] = monEnd.Value;
            monSchedule[2] = monLunch.Value;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            setDefaults();
        }

        private void setDefaults()
        {
            monStart.Value = DateTime.Parse("00:00");
            monEnd.Value = DateTime.Parse("00:00");
            monLunch.Value = DateTime.Parse("00:00");
        }

        private void validate()
        {
            MessageBox.Show("You entered time " + monSchedule[0]);
        }

When I load my form, setDefaults(); should change the values to the current date with a time of 00:00. When I press the button to show the value in the array, it is pulling current date and current time. I need it to pull whatever the current time in that DateTimePicker is. So if a user types 10:00 into the DateTimePicker (they are formatted HH:mm), then I need the MessageBox to say the time is 10:00 AM. If I change the value to 22:00, then I need the messagebox to say the time is 10:00 PM. etc. (Date is irrelevant in my scenario, I'm not concerned with what the date is at all. Only the time.)

I suspect it may be because of the order it's written in. Is the array storing the value of the DateTimePicker BEFORE setDefaults(); is run? If so, how do I make the values of the array items dynamic since the values of the DateTimePickers are going to change a lot and I need the array elements to be updating with the latest values?

EXTRA INFO: -Using Visual Studio -Added the DateTimePickers in design view, changed the format to HH:mm there, did not change the default values in design view -Ignoring date completely, only concerned with time right now

PS: I was also struggling with where to declare the array so it was accessible in multiple other methods and found I had to declare the array initializer within public partial class Form1, but then add the items in the array within public Form1(), because it wouldn't let me add them under public partial class Form1. I don't know if this is correct though, but it seemed to work when I tested with an array of strings so I went with it.

I have to say that this is a bit of a regression. In your previous question, JoshPart gave you good advice in the form of user controls, although he may have left some gaps too large for you to fill on your own.

Using arrays in this manner might work for a single day, but it won't scale well to a full week.

In case anyone reading this is wondering why I'm talking about a full week, I refer you to the previous question. Also, I recognize that I'm going off-topic for this specific question but I believe this to be an XY problem and the previous question was actually based on the real problem and work that was more on-the-mark.


Let's start with what we know. I've gleaned this from the two questions and the various comments in both.

  1. You have DateTimePicker controls for start, end, and lunch. You're only interested in the time portion so you have Format set to "Custom" and CustomFormat set to "HH:mm". Assumption: lunch is a fixed length so the end time isn't needed.

  2. You have the aforementioned controls times seven, one set for each day of the week.

  3. You've written validation code (range tests) to determine if values are entered correctly, and you're able to show a label with red exclamation marks when that test fails.

  4. You've identified that it's getting too complicated just having a bunch of controls on a form.

So far, so good. Now for your goal.

  1. You're looking for a way to organize the controls, and the data they collect, to make it easier to work with them.

A user control is still the way to go here. You'll benefit from encapsulating all that repeated functionality into a single place and being able to reuse it.

Start by creating a user control -- we'll call it DayPanel -- and put all the controls for a single day on that canvas. Name the controls without any regard for the day of week (e.g. start, lunch, and end). Your user control will neither know nor care which day it represents.

Add an event handler for the ValueChanged event to the DateTimePicker controls. Instead of double-clicking the control, go to the events list in the Properties tool window and type a name, such as the one below, for the ValueChanged event. Do the same for the other two controls and it will reuse the event handler that it created the first time. Whenever the user changes a time, this event handler will be called and it will effect changes to the UI.

private void picker_ValueChanged(object sender, EventArgs e)
{
    // In case you need to know which DateTimePicker was changed, take a look at 'sender':
    //DateTimePicker picker = (DateTimePicker)sender;

    UpdateWarningState();
}

As Jimi mentioned, the sender object will be a reference to the DateTimePicker control that sent the event. You probably won't need it but it's there if you do.

UpdateWarningState just hides/shows the warning label based on the validity of the inputs.

private void UpdateWarningState()
{
    warningLabel.Visible = !IsInputValid(start.Value.TimeOfDay, lunch.Value.TimeOfDay, end.Value.TimeOfDay);
}

I had suggested in comments on the previous question that it seemed to make sense to get true if the inputs are valid and then use the logical negative for the visibility of the warning label.

As Paul Hebert pointed out, you really only need to compare a TimeSpan, so IsInputValid receives the TimeOfDay property to deal with only that much.

private bool IsInputValid(TimeSpan startTime, TimeSpan lunchTime, TimeSpan endTime)
{
    return startTime < lunchTime && lunchTime.Add(TimeSpan.FromMinutes(30)) < endTime;
}

In fact, even though you're only inputting a time, the control still returns a date part in its Value property. If you want to be certain that you're not comparing times in different dates, you'll definitely need to use the TimeOfDay property. That said, by not presenting the date part, you have a measure of control over that so it's not a pressing concern. If you had to worry about crossing over midnight, that would complicate things.

Note that I've dealt with that earlier assumption that lunch is a fixed length by adding 30 minutes in the comparison to the end time.

Why not just do all that in the ValueChanged event handler?

  1. The Single Responsibility Principle. IsInputValid does one thing: business logic; it tells you if the inputs are valid based on range testing. UpdateWarningState does a different thing: UI logic; it updates the visibility of warning label based on the validity of the inputs.

  2. UpdateWarningState is reusable. You can call it from other event handlers in the future. Event handlers really shouldn't ever do much. They're more like telephone operators: "how may I direct your call?"

  3. IsInputValid is reusable. The business logic can be extracted from your UI code at some point in the future and be reused by something else. I'll admit that the name leaves something to be desired; it fits here but probably should be different outside this context.

But what good is this user control if you have no way of working with its data? The consumer needs to be able to interact with it. A user control is just another class so you can define public properties, methods, and events as you see fit. We'll add properties for the three values of interest:

public TimeSpan Start
{
    get => start.Value.TimeOfDay;
    set => start.Value = start.Value.Date + value;
}

public TimeSpan Lunch
{
    get => lunch.Value.TimeOfDay;
    set => lunch.Value = lunch.Value.Date + value;
}

public TimeSpan End
{
    get => end.Value.TimeOfDay;
    set => end.Value = end.Value.Date + value;
}

What's interesting to note about these properties is that they don't have their own backing storage. Instead, they defer to the controls and translate between their own TimeSpan data type and the controls' DateTime data type. On get, they return just the TimeOfDay property. On set, they remove the time portion (with .Date) and add the time of day.

If you were building this for someone else to consume, you'd want to ensure that the Days property is 0 and that the whole value is non-negative, and either throw ArgumentOutOfRangeException or (gasp!) clamp the value to the acceptable range.

Now that you have a functioning control for a single day, you can slap a bunch of them on the main form. Back in Form1, add seven instances of the DayPanel control and name them monday through sunday. Before we get to initialization, let's create a lookup for these user controls.

private readonly Dictionary<DayOfWeek, DayPanel> _dayPanelLookup;

public Form1()
{
    InitializeComponent();

    _dayPanelLookup = new Dictionary<DayOfWeek, DayPanel>()
    {
        [DayOfWeek.Monday] = monday,
        [DayOfWeek.Tuesday] = tuesday,
        [DayOfWeek.Wednesday] = wednesday,
        [DayOfWeek.Thursday] = thursday,
        [DayOfWeek.Friday] = friday,
        [DayOfWeek.Saturday] = saturday,
        [DayOfWeek.Sunday] = sunday
    };
}

Now the Load handler can then initialize all the properties. This DefaultTime duplicates the TimeSpan.Zero constant for the purpose of giving it a distinct meaning and can help with refactoring later on.

private static readonly TimeSpan DefaultTime = TimeSpan.Zero;

private void Form1_Load(object sender, EventArgs e)
{
    SetDefaults();
}

private void SetDefaults()
{
    foreach (DayPanel dayPanel in _dayPanelLookup.Values)
    {
        dayPanel.Start = DefaultTime;
        dayPanel.Lunch = DefaultTime;
        dayPanel.End = DefaultTime;
    }
}

And just for fun, we can use _dayPanelLookup to grab one of them based on a variable containing the day of the week.

public void someButton_Click(object sender, 
{
    DayOfWeek whichDay = SelectADay();

    DayPanel dayPanel = _dayPanelLookup[whichDay];

    // ...
}

That should address the main concern of organizing the controls and making it easy to work with them and their values. What you do with it once the user presses some as-yet-unidentified button on the form is a whole new adventure.


There are yet better ways of doing all of this, I'm sure. I'm not a UI developer, I just play one on TV. For your purposes, I hope this not only gives you the guidance you needed at this point in this project but also illuminates new avenues of thought about how to structure your programs in the future.

Arrays that represent points in time - MATLAB, t = datetime( DateStrings ) creates an array of datetime values from the text in All values in the input argument DateStrings must have the same format. then specify the appropriate time zone for t using the 'TimeZone' name-value pair argument. If you do not specify a time zone, then the POSIX times or Julian dates in X  '', to create an “unzoned” datetime array that does not belong to a specific time zone. The name of a time zone region from the IANA Time Zone Database; for example, 'America/Los_Angeles'. The name of a time zone region accounts for the current and historical rules for standard and daylight offsets from UTC that are observed in a geographic

It is unclear what you want the date component of the date part of DateTime to be DateTime.Parse("00:00") should return midnight today or 12/27/18 12:00:00 AM; This is also the same value as DateTime.Today In addition, you can create a new DateTime with a constructor

monStart.Value = new DateTime(2018, 12, 27, 0, 0, 0);

This is midnight the today

Not-a-Time - MATLAB NaT, representation for Not-a-Time, a value that can be stored in a datetime array to You also can assign the character vector, 'NaT' , to elements of an existing Use the NaT function to create a new datetime array containing only NaT values. Note. An alternative to the DateTime structure for working with date and time values in particular time zones is the DateTimeOffset structure. The DateTimeOffset structure stores date and time information in a private DateTime field and the number of minutes by which that date and time differs from UTC in a private Int16 field.

datetime - laminas-form - Laminas Docs, Laminas\Form\Element\DateTime is meant to be paired with the to its input filter specification in order to validate HTML5 datetime input values on the server. input specification for the element may not contain the correct validation rules. setOptions(array $options) : void, Set options for an element of type DateTime . I am trying to check if an Array A contains all the elements of Array B and similarly Array B contains elements of Array A, the order does not matter. Some examples: A B Result {1} {0} false {0, 0, 0} {0, 0, 0} true {17, 4, 11} {4, 17, 11} true

Fusebox: Developing ColdFusion Applications, (Lists can store multiple values, but because they are actually strings, they Table 7.1 Datatypes Simple Complex String Array Number Structure Boolean Recordset Datetime Although they are essentially simple, they are not really datatypes per se. <out> Element The <out> element is used to contain elements for any  Gets the value of the element as a byte array. GetDateTime() Gets the value of the element as a DateTime. GetDateTimeOffset() Gets the value of the element as a DateTimeOffset. GetDecimal() Gets the current JSON number as a Decimal. GetDouble() Gets the current JSON number as a Double. GetGuid() Gets the value of the element as a Guid. GetInt16()

Dynamic HTML: The Definitive Reference, ELEMENT_NODE) { // operate on an element } } Value Array of node object For example, document.body.children[] might contain a form, but no reference to property (along with dateTime) is shared among all phrase element objects in IE​  There are cases when you would use each one of these: an optional attribute if you have a simple, nullable type; a minOccurs="0" element if you have a complex nullable type and you want it to take up the least amount of space; a nillable="true" element if a null value must have a placeholder (for instance, when it appears in an array).

DateTime.ParseExact Method (System), of a date and time to its DateTime equivalent using the specified array of formats, s does not contain a date and time that corresponds to the pattern specified in format . Sun 15 Jun 2008 8:30 AM -06 is not in the correct format. with no time element and the parse operation succeeds, the resulting DateTime value has  TRUE/FALSE: When an array of objects is declared, but not initialized, the array values are set to null. True TRUE/FALSE: A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.

Comments
  • In your setDefaults() you set the values in the 3 dtp, you are not changing the values in the array. Instead the array is initialize in the constructor (before the Form_Load event) with the current value of the DateTimePicker. This default value is the current date and time.
  • I can't quite follow your code (since it's incomplete). Where do you initialize monStart, monEnd and monLunch. Whatever you put in those values before you construct an instance of Form1 will be what you put in the array. The other thing you need to remember is that DateTime is a Value Type, instances of DateTime get copied by value (like an int or float) and not by reference. Once you stick something in the array, if you change what you copied from, it doesn't change what's in the array
  • @flydog57 They were added in Visual Studio designer, so I guess Visual Studio did all of that initializing. I'm sorry for the confusion, my previous posts were not well received when I included extra information like that, so I just stripped it down this time.
  • Set the DTPs Value (monStart.Value = DateTime.Parse("00:00"); etc.) in the Form.Load event. Add an Event Handler (the same) to the ValueChanged event of the DTP controls. Use the sender object to determine which DTP raised the event. Set the value of the DTP to the corresponding array index in the event handler. Show a MessageBox if needed. Your monSchedule array is a field, accessible anywhere in the Form1 class.
  • @Jimi I'll have to look into this sender object. I already set the DTPs value in the Form.Load event via the setDefaults() function that you see called. I'm familiar with using the ValueChanged events because that is a HUGE portion of my overall code that this array is going to get incorporated into. But the sender object is new to me. If you have time, could you link a good resource on using it?
  • I didn't include a lot of other details that may be relevant here.. Date is completely irrelevant. We're working only on times. But it's going to store a date value anyway so I just ignore it. I need the array to store the most CURRENT value - be that changed by a method in the program (like setDefaults) or changed by the user. For example, if I type 10:00 into the DateTimePicker, I want MessageBox to show 10:00 in the time. I'll edit my post with this clarification.
  • Instead of using DateTime use a TimeSpan. You could then just store time. Time span starts at 0:0:0 docs.microsoft.com/en-us/dotnet/api/…