How to set the date of bootstrap-datepicker? - javascript

I've set up this JSFiddle showing my problem.
$('#dp3').datepicker();
var date = new Date('2009-05-05');
$('#dp3').datepicker('setDate', date);
It seems like a rather simple thing to do, and I feel I may be overlooking something, but this should work, shouldn't it? I'm simply trying to set the date of the calendar to a specific day.
I know there are methods to use to set the start date in the HTML mark-up, but this is not what I need to do. This needs to be done in JavaScript.
There's documentation here that I looked over, but the "setDate" option doesn't seem to do as it says it does.
Thanks.

Try $('#dp3').datepicker('setValue', date); or $('#dp3').datepicker('update', date);

Try this...
$(document).ready(function(){
$('#dp3').datepicker();
var date = new Date('2009-05-05');
$('#dp3').datepicker('setDate', date);
});
See you DEMO

Related

How to detect changes with Date objects in Angular2?

Date objects that are modified using setDate method arent getting updated in template.
In template:
<p>{{date | date:'mediumDate'}}</p>
In component:
nextDay(){
this.date.setDate(this.date.getDate()+1);
}
But when I call nextDay function, the template isnt updated with the new value.
The only way I could get the change detection working was doing this:
nextDay(){
var tomorrow = new Date();
tomorrow.setDate(this.date.getDate()+1);
this.date = tomorrow;
}
Are there a better way to accomplish this same task?
I think that is the right way, to change the reference of the date variable. From the docs here we have:
The default change detection algorithm looks for differences by comparing bound-property values by reference across change detection runs.
So if the date reference remains the same, nothing will happen. You need a new Date reference and that's why the second version of nextDay() works.
If you remove the formatting pipe you will see that still only the second version of nextDay() works.

Dates - JavaScript and C#

I hate dates, I can never get them to behave.
I have a javascript variable that looks like this:
var currentDate = new Date();
I pass this to a C# Web API controller as a parameter.
My local time was 12:43 but when I put a breakpoint in my action it shows 11:43. The problem is, that if I do this at 00:43 then my controller would take the date as yesterday. I need it to pick out the right day. If I select the currentDate as 02/09/2015 12:43 then I need my controller to use the same date.
I know this has something to do with local times etc, but how can I get them all to use the same one?
Any help would be greatly appreciated.

how to find hh:mm:dd difference between two strings in Javascript

In my app I have two date strings:
Say that they are:
date1 = "2014-03-14 18:25:15";
date2 = "2014-03-14 16:26:15";
I get these date strings based on two events that the customer selects. Now I need to show the difference between these two strings in HH:MM:DD format.
What I am currently doing is, posting to PHP using AJAX and then doing the calcuation in the server:
$rDate = new DateTime($date1);
$tDate = new DateTime($date2);
$interval = date_diff($rDate,$tDate);
echo $interval->format('%h:%i:%s');
Then in the AJAX response handler I print it to a div
My problem is that server trip is just too much an overkill for this. How can I achieve the same thing from browser itself? (Javascript/Jquery/MomentJS)...
Any help is greatly appreciated!
I'd suggest looking into Moment.js, which is a very well featured date handling library for Javascript.
Here's the relevant manual link for Moment.js for what you're wanting to do: http://momentjs.com/docs/#/displaying/difference/
Hope that helps.

Better to use declarations or self to modify variables

This is probably a stupid question, but im wondering which is better practice for JS
var expireDefault = new Date();
expireDefault.setYear(expireDefault.getFullYear() + 1);
This is how i am getting next years date at run time.
I have been told however to use a seperate Date() decleration.
var today = new Date();
var expireDefault = new Date();
expireDefault.setYear(today.getFullYear() + 1);
Is this necsecary ? Or does it even matter ?
In my opinion from this example i dont circular reference.
But the principles remain the same.
The two are equivalent.
The second one is just uselessly verbose and heavy. Don't use it : it hides the simplicity of what is done.
The second one is only necessary if you want to use later today's date. If not, you are unnecessarily using memory to store a Date object and so, first option is better.
I would prefer the first approach, it reads better, but that's just an oppinion. Take a look at momentjs, which is an amazing lib dealing with dates.

Bootstrap Timepicker

I am trying to find a time picker widget, compatible with bootstrap styling. I really like the style of the jdewit widget, but it has a tremendous number of bugs. I am trying to finish this project quickly, so I don't want to get bogged down in fixing library bugs.
Can anyone else recommend a slick timepicker that is production ready?
I'm the author of the jdewit/timepicker.
I finally took some time to get through all the reported issues. Hope it meets your needs.
You could try our one https://github.com/Bloafer/bootstrap-timepicker
We are now using this one https://github.com/Eonasdan/bootstrap-datetimepicker as it is being actively developed
I tried to use https://github.com/Bloafer/bootstrap-timepicker, it didn't find it very helpful as it needed to have the time format as HH:MM, whereas mine were being supplied as HH:MM:SS. Also the styling is not the best.
This is also a problem for me, I have tried 4 timepickers and all of them have some kind of issue under bootstrap, I would like to see an answer to this as well
You could try this: https://github.com/jonataswalker/timepicker.js
I'm the author of this thing.
You'd use with something like this:
var timepicker = new TimePicker(['field1', 'field2'], {
theme: 'dark', // or 'blue-grey'
lang: 'pt' // 'en', 'pt' for now
});
timepicker.on('change', function(evt){
console.info(evt);
var value = (evt.hour || '00') + ':' + (evt.minute || '00');
evt.element.value = value;
});

Categories

Resources