Suppose I have a DateTimePicker with a CustomFormat of MMM-yyyy which has been initialised to a value of 31st Oct 2013, this will display as Oct-2013.
If the select the Oct section of the control and press either up or down arrow, this generates an ArgumentOutOfRangeException - Year, Month, and Day parameters describe an un-representable DateTime.
Presumably it is changing the month without changing the day and there are only 30 days in September and November. Note, if the CustomFormat is dd-MMM-yyyy then no error is thrown because the day is automatically changed to the 30th.
How can I avoid or catch this error?
I can add code to ensure that the DateTimePicker is always initialised to the first of a month, but I want to allow the user to select the month and date from the calender dropdown, so I need to cope with the situation where the user has manually selected the 31st and then tries to change the month using the keyboard.
Just initialize it to Oct 1st. No exception, still the same display.
And of course you'll need to adjust the value picked by the user to keep it on the 1st:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
var dtp = (DateTimePicker)sender;
dtp.Value = new DateTime(dtp.Value.Year, dtp.Value.Month, 1);
}
Yeah I know that answer is already posted and accepted by OP. but, when i have tried this solution it was not working form me.
Ok, let's take an example: If I set the date format for DateTimePicker to MM/yyyy and I am trying to select month from 09 to 10 (September to October) by pressing Down arrow key Then, it will throw the exception and will bring me at the Program.cs file.
The solution is same as given by Hans Passant but, the only difference is that just we need to use PreviewKeyDown event instead of ValueChanged. I don't know whether KeyDown event also works for this. But, I have tested it with PreviewKeyDown event and it was working fine.
Related
I set the minDate(2019-06-21) and maxDate(today) when initialize the "From" and "To" datetimepickers. When select time interval to month, Will format the datetimepicker to "MMM YYYY". Then I only can select Jul in the calendar widget although the Jun is also active and should be selectable.
But if I add "useCurrent: false" in the datetimepicker initialization code, the calendar will allow user to select Jun only but not Jul.
code is here:
enter code here
https://jsfiddle.net/srcuz3fm/
Since the minDate is Jun 21, 2019 and maxDate is today(7/23/2019), it should all user to select either Jun or Jul from the calendar widget no matter the useCurrent is true or false.
After days of trys, I figures out how to fix this issue. BAsically, the reason cause the problem is when change datetimepicker format, it'll set the year to the morning midnight of new year day. Since I set the minDate in the initial settings which is "2019-06-21", so the datetimepicker won't allow me to select 2019.
So I added these in the javascript together right before the format,
$("#datetimepicker2").datetimepicker("minDate", moment(startdate).startOf("year"));
$("#datetimepicker3").datetimepicker("minDate", moment(startdate).startOf("year"));
$("#datetimepicker2").datetimepicker("maxDate", moment(today).endOf("year"));
$("#datetimepicker3").datetimepicker("maxDate", moment(today).endOf("year"));
Here is the link to working code in jsfiddle
When you format the datetimepicker to {'format', 'L'}, the time will be set to 00:00:00. Add 86399 seconds to maxDate to make the last day working:
maxDate: moment("24.12.2021", "DD.MM.YYYY").add(86399, 'seconds'),
I need to pick a date from blueprintjs using datetimePicker. The point is that when I select any day (for example 2017-11-11):
Using mouse click a get resulting time 12:00 am. and this component looks at my timezone (+2) and takes this into acount. As a result I got date --> 2017-11-11 10:00.
Using keyboard time is 00.00. I got date --> 2017-11-10 22:00. this is the previous day.
if it is a bag maybe there is a scenarious where I can skip timezones. Because I do not need time... Only date.
If you don't need time then use DatePicker.
I am using this calendar https://github.com/mattlewis92/angular-bootstrap-calendar for showing events and time slots for people all over the world. During sign up, every user has to set his timezone and then my application uses this timezone for further date computation rather than client machine timezone.
The problem is that when I make user timezone default using moment.tz.setDefault(timezone) and change the machine's timezone, the calendar calculates dates wrongly.
Here is my excerpt of my code:
moment.tz.setDefault($rootScope.timezone)
vm.calendarView = 'month';
vm.viewDate = moment().startOf('month').toDate();
vm.cellIsOpen = true;
Attached is the screenshot:
[Screenshot]
You can see that user's timezone is currently Asia/Karachi +5 and my machine's timezone is Beijing +8. Today's date is 8 September and the day is Friday, but on the calendar 8 September is showing as a Saturday instead of Friday.
mwl-calander did not provide support for timezone, you can use full calander
https://fullcalendar.io/
Demo https://fullcalendar.io/js/fullcalendar-2.9.1/demos/timezones.html
Its angular directive can be found at
https://github.com/angular-ui/ui-calendar
It would appear that this particular UI control does not support selecting a time zone. Simply using moment.tz.setDefault is not good enough, because everything the control is doing both internally and in its external API is using Date objects, which cannot represent arbitrary time zones. In other words, the author of that control would have to remove all .toDate() calls and use Moment objects as the primitive in the control instead of Date objects. That would be a breaking change for them.
I suggest filing an issue in that project's GitHub repository, and reference this page.
Given a ui control that when you select 01/01/2017 it returns a Javascript date as "Sun Jan 01 2017 05:00:00 GMT+0000" (note, an actual Javascript Date object) so its not a string...
However, I definitely need the date part (01/01/2017) and "as UTC"
C# has a DateTime.SpecifyKind(datetimevalue, DateTimeKind kind) method
Basically whats the equivalent to this that doesnt convert it:
moment(vm.theDate).specifyKind(utc)
Looking for to get a moment instance that is 01/01/2017, isUtc=true, from that type of Javascript Date UI control. (Its the Angular ui-datepicker control)
You should look in the UI control's docs for a way to retrieve the selected value as a string instead of a date object. A date object will be influenced by the local time zone, which will throw off your results - especially around DST transitions. Many good datepicker controls will offer this as a separate property (ex, theControl.value or theControl.text instead of theControl.date). Without knowing the specific control you are using, I can't offer anything more specific.
Once you have the string, use moment.utc(yourInputString, yourInputFormat)
I have problem with richfaces calendar. I want to see only year and month so i made datePattern="yyyy/MM", which works fine until i want to change date.
When i click on button and calendar pop up the current value disappear. If i don't select date the value is null. I tried to save old date in my bean but problem remains because when i open calendar it is always set to todays date.
Problem occur only when i don't put dd(days) in datePatern
<rich:calendar value="#{myBean.date}" datePatern="yyyy/MM" />
Tnx for help in advance
I got answer on richfaces forum so i will copy it also here
I guess rich:calendar is not the right choice at you case although rich:calendar may be bound to probably any object type provided that you supply custom converter a day ordinal still should be supplied because it's a part of the date maybe you would do better with a h:selectOneMenu to specify a month and a h:inputText to define a year instead of "incomplete" rich:calendar