Set month view for `bootstrap-datepicker` without setting date - javascript

Is there a way to set the current view of the calendar to a specific month/year, without setting a date?
My use case for this is when a user selects a date, I save this date, and clear the calendar by calling clearDates() on it. However, if the selected date fell on a month in the future, (for example, we're in November, and the selected month is next March), then the calendar's view resets back to the current month (November).
I want to set the calendar's view to the last selected month (March), without setting the date, so that the calendar has no date selected, but is showing the days for March of next year (to allow the user to select another date in the last used month, instead of having to click Next bunch of times to get to March again).

You could use the defaultViewDate option for this behavior
$('#datepicker').datepicker({
defaultViewDate: {
year: 2016,
month: 2
}
});
The months int is used like the javascript date month. So january = 0 and december = 11
See example here: http://jsfiddle.net/zcvq0yhh/4/
If you want to change it on the fly there is no built-in method (you can create a feature request here)
But this is an workaround to do it: http://jsfiddle.net/zcvq0yhh/5/

Related

How to detect first day of a month with Day.js

I am building a calendar component in React without using any library but Day.js.
I need to detect the first weekday of the month in order to leave a gap as shown in the picture. Otherwise the first day of every month has to be Sunday. How can I detect the first weekday of the given date?
Day.js has built in function to achieve this
dayjs().startOf("month").day()
This method returns current month's first week day as number.
0 = Sunday,
6 = Saturday

angular.js moment previous/next month navigation

I need to make a navigable calendar and i'm using angular moment library for date calculus.
I have the calendar but I need to make the previous and next month buttons. For now, I managed to find out the previous month but when I call the previous function, it will always return August (since it's subtracting 1 month from the current one I guess). How do I make this recursively using angular moment?
This is what I have and it will always return the previous month from september in milliseconds (it has to be in milliseconds).
This is my code:
$scope.firstDayOfTheMonth = moment().subtract(1, 'months').startOf('month').valueOf();
And I need to get the first day of the previous month recursively.

JavaScript setting dates in textboxes not consistent

I have a couple of textboxes where a user set a from date and to date from a button click which adds 7 days or substracts 7 to whatever is the current value in each box.
When the page is first loaded the dates that are added into the textboxes are based upon the user belonging to a group. Thus if a user belongs to group A, the from date is a Sunday, but if the user belongs to group B it is a Friday. This logic I set in the page load event in my ASP.Net page.
The situation that is baffling me, is whilst I can set the dates, and get my JavaScript to work with group A, if a switch the user to group B, and click on a button rather expecting the date in from changing from 17th January 10th January, it jumps to 29th December 2014. It seems to be getting a completely differently value initially 5th January 2015.
The JavaScript I have is:
var fromDateIn = new Date(formatDate(document.getElementById('<%=txtFromDate.ClientID%>').value));
var newfromdate = new Date(fromDateIn);
In one of the button event, I have:
function setNewFromDate() {
newfromdate.setDate(newfromdate.getDate() - 7);
document.getElementById('<%= txtFromDate.ClientID%>').value = formatDate(newfromdate);
var toDate = new Date(newfromdate);
document.getElementById('<%= txtToDate.ClientID%>').value = formatDate(toDate.setDate(toDate.getDate() + 6));
}
As I say, everything works perfectly and I get the right dates when the user is one group, but as soon I change the user to another group and set the initial from and to dates, I get this problem. Can anyone please advise I can get consistency in this?
Thanks
There is a problem with the way you are instantiating fromDateIn. You are passing it a value in the format dd/MM/yyyy, but the date constructor that takes a single string value is locale dependent and is treating the input as MM/dd/yyyy. This results in a completely bogus value for fromDateIn that throws everything else off.
You should find a way to determine the year, month and day for the start date (either by parsing it out of the textbox value or having the ASPX logic stuff it into the page somehow). And instantiate the date using the new Date(year, month, day) constructor. (This requires bearing in mind that month is 0-based [i.e. January = 0, February = 1]).
This line:
newfromdate.setDate(newfromdate.getDate() - 7)
Looks like you're trying to move back a week. This will fail. setDate changes only the Day of Month. Can you guess what happens when Day of Month is less than 7?

how to validate and add date using jquery

I am working on a health website.
I am having a field called Last Menstrual Period, its a textbox which has to be filled in by doctor in format of YYYY-MM-DD.
What I want to do is, I have to add 281 days into the LMP date that the doctor will be entering in order to generate the Expected Delivery Date (Child Birth)
So I need followings thing to do:
The date entered should be in format of YYYY-MM-DD
It should be a valid date i.e. taking care of leap years and invalid dates like 2013-02-31 etc.
After generating the Expected Date of delivery (based on LMP that is entered), it should be displayed on screen.
As soon as the date is entered in the LMP textbox, these validations should be performed and the Expected Date Of Delivery is displayed inside a tag below the LMP textbox
How can I do that? Here is what I have tried so far.
// Calculate Expected Date Of Delivery
$('#lmp_date').change(function()
{
var lmp_entered = $this.val();
if(lmp_entered)
{
// $('#edd').html('1');
}
else
{
// $('#edd').html('Please enter last menstrual date to calculate EDD');
Please help me how to implement these validations and display the date using jquery. I am a newbie in jquery so dont know much about it. Any small help will be highly appreciated.
for validation I don t recommend using another library especially for such a simple rule. if the rule is so strict just write your own and use it site wide.
For example block non-numberic entries to textbox and add dash "-" by your code in every 5th and 9th chars.
Then to validate check the length of text and dashed, use split to get year. month and day separately and convert it to date with Date(txtYear,txtMonth,txtDay) if your date's year, month and day are equals to your txtYear, Month and Day then it means it s a valid date. After that use the below to calculate birth date.
just use Date constructor like below
var delDate = new Date(year, month, day + 281)
Just don t forget months start 0 in JS so Jan = 0, Feb = 1 etc.

Javascript Date acting weird

Okay, I have heard about it but I can confirm now that the Javascript Date functionality is a disaster zone. And I have created a monster out of it. I have this Program :
A JSON object contains list of holiday dates and its respective label.
I need to find out the date of 5 business days from today (excluding saturday, sunday and holiday if any which is contained in the JSON object.) Good stuff so far. Then this 5 business days' date is going to be devoured by the jquery calender as a default selected date which is not included in the fiddle as it is irrelevant. (Note: the start date on the calender is tommorow's date) Good stuff again. THEN, comes this part: If it is before noon today, I can select tommorow else start date is day after tommorow. I'm elaborating this because it is included in this fiddle.
So the problem is multiple initialization of the function which handles above functionality is not producing consistent result. It was calculating 5 business days on my system, but when i made this fiddle, it is calculating 4. The date of "5th" business days is incremental by 1 on each call.
http://jsfiddle.net/xXQ7j/27/
Anyone!
Your problem is probably caused by timezone issues.
Whenever possible you should use new Date(y, m, d) to create a date object, rather than supplying a string. In particular, I've found that you get a date relative to 00:00 UTC if you specify a string in format yyyy-mm-dd but one relative to local midnight if you use yyyy/mm/dd.
In any event, I would suggest a different approach:
convert your holiday date into an object, with the date being the key
generate today's date
if it's after noon, get tomorrow's date - d.setDate(d.getDate() + 1)
create an empty array
add one day (per #3 above)
check if the new day is Saturday or Sunday, if so, go back to #5
check if the new day is in the holiday list, if so, go back to #5
add the new date to the array
repeat until you have 10 entries
That should give you the next 10 business days in your array. Pick the ones you need to fill out your date picker.

Categories