Angular-UI Bootstrap Datepicker new date - javascript

I am using Angular-UI Bootstrap Datepicker directive for my project. What i want to do is, when i click to button, datepicker value is updating as expected, however, datepicker popup shows the wrong day. I am using dd.MM.yyyy format. It shows the mm.dd.YYYY instead of dd.MM.yyyy. is it a bug or am i missing something?
function appConfig(datepickerConfig, datepickerPopupConfig) {
datepickerConfig.startingDay = 1;
datepickerPopupConfig.datepickerPopup = 'dd.MM.yyyy';
}
Plnkr

The date picker works just fine in my understanding. The problem is that you change the value in your controller with this.date = "01.10.2015".
You might consider doing this with this.date = new Date(2015,9,1) because date is of type date in the background. The directive cares for the transformation of these formats.
EDIT:
If you want to stick to your way, you could use a parse function like this (found it here):
function parseDate(input) {
var parts = input.match(/(\d+)/g);
return new Date(parts[2], parts[1]-1, parts[0]);
}
And the call:
this.date = parseDate("01.10.2015")
Here is your updated plunker.

Related

getEventsForDay() formatting issues with datepicker return value

I'm trying to use the Calendar API to pull up some events based on datepicker output. The issue that i'm facing with my AppScript is formatting properly the value that i get from the datepicker that will serve as input for the getEventsForDay() function.
function testing(){
var z = CalendarApp.getCalendarsByName("X")[0];
var date = new Date('2016-10-12');
var dateformatted =Utilities.formatDate(date), "GMT", "yyyy-MM-dd hh:mm:ss a");
var a = z.getEventsForDay(dateformatted, {search: 'OOO'});
The output of a into this scenario is a empty object - which is expected because this formatting is not working at all. (i've read 1000 posts that this should work).
For context as well, i have one working example with today's date, which it works fine becase the input is a new Date(). Here you go:
var datetoday = new Date();
var b = z.getEventsForDay(datetoday, {search: 'OOO'});
Any ideas on what i'm missing here?
Thanks in advance.
Since new Date() returns the Date object in UTC and not the local timezone, you may be querying the wrong day and hence the empty object if the other day does not have any events.
You can covert the date to the current timezone like
date.setTime(date.getTime() + date.getTimezoneOffset()*60*1000)
This should return the date in the local timezone and you will get the events for the correct day.
Hope it helps
Made some changes and now this works on the App Script:
var t = "2016-10-01";
var q = t.replace(/-/g,"/");
var a = new Date(q);
a.setTime(a.getTime() + a.getTimezoneOffset()*60*1000)
var w = z.getEventsForDay(a, {search: 'OOO'});
Logger.log(a);
Logger.log(w);

Converting new Date() to a format that includes T

Background
I have a datepicker (Angular UI) defined like this:
<date-picker name="contractEndDate" date="employee.contractEndDate"></date-picker>
The contractEndDate is set by a button click, changing it's value from null to today.
The date is set using this:
self.HandleEmployeeEndOfContract = function () {
$scope.employee.contractEndDate = new Date().toLocaleString();
}
At the time of writing, that gives me:
26-1-2016 15:09:55
What I need
The datepicker needs the date as 26-1-2016T15:09:55 (notice the extra T). Setting contractEndDate to a date without the extra T, doesn't seem to work.
Question
Can I change the .toLocaleString() function to something that gives the date as above (incl. T)?
This is UTC format and you can achieve this using new Date().toISOString();
new Date().toISOString()
=> "2016-01-26T14:24:42.974Z"

JQuery DatePicker ignores DateFormat

I have a datepicker component in my C# app, which takes its default value from the server through AJAX call. The problem is, it ignores the date format I've defined as a parameter. I looked into the code and it seems that the date format is not set on the settings variable, so JQuery uses the default value (mm/dd/yy).
Here is my code:
$.when(_getUserProfileValue(defValue))
.done(function (result) {
// result is 23.07.2015
// this line shows the correct date and format
$("#divTest").text(result.d);
// new Date(res) gives back invalid date, but datepicker shows the result from server
// without this line, datepicker shows a completely wrong date
// TODO: format is still ignored
var aDate = new Date(result.d);
$calendar.datepicker({
dateFormat: "dd.mm.yy"
});
$calendar.datepicker("setDate", aDate);
})
Any ideas?
Might be because you have already initialized the datepicker on that element, in that case to update the date format of an existing datepicker use the option method like
$calendar.datepicker('option', 'dateFormat', "dd.mm.yy");

Full Calendar Get current date

I've been learning Ruby over the last year and I'm very new to JS so I'll try to explain this as best I can.
I am using Adam Shaw's full calendar plugin. All I want to do is get the current month I am viewing (and use that to limit how far in the future or past a user can navigate, but that's not the problem).
I can get the current date, sort of. But, because of my lack of JS knowledge I'm not sure how to access the date.
Here is the relevant section of the config file,
viewRender: function(view){
var maxDate = "<%= finish.strftime('%Y/%m/%d') %>";
var currentDate = $('#calendar').fullCalendar('getDate');
console.log(currentDate);
if (view.start > maxDate){
header.disableButton('prev');
}
}
When I inspect the console log I see this being output as I click through the months.
So as you can see it is displaying the current date in view. My question is how do I access the _d bit of the Moment variable so I can use it?
My understanding would be that the Moment is class instance and the stuff in the dropdown is like its attributes, would this be a correct interpretation?
To get the current date of the calendar, do:
var tglCurrent = $('#YourCalendar').fullCalendar('getDate');
This will return the date as a moment object. You can then format it as a string in the usual date format like so:
var tgl=moment(tglCurrent).format('YYYY-MM-DD');
For the actual time, use the format: YYYY-MM-DD LTS
FullCalendar's getDate returns a moment object, so you need moment's toDate() method to get date out of it.
So, in you code try:
console.log(currentDate.toDate());
and that should return a date object.
var moment = $('#YourCalendar').fullCalendar('getDate');
var calDate = moment.format('DD.MM.YYYY HH:mm'); //Here you can format your Date

JQuery Datepicker change value attribute of div when current date is selected

The jQuery Datepicker is working great, but I would like the submit button to read different text based on whether or not the day selected is today. The submit button is a <button> and the text is the value attribute.
Is there any way to do this? My web searches haven't turned up any results.
Try like below
var dp = $('#datepicker').datepicker("getDate");
1) using the above statement, you can get the date that has be selected in datepicker (ie., parsed as date datatype)
2) Using date.toDateString(), you can get the date string which can be used to compare with datepicker's date.
$('#datepicker').datepicker();
$('button').on('click', function () {
var dp = $('#datepicker').datepicker("getDate");
var today = new Date();
if (today.toDateString() == dp.toDateString()) {
//write your code to change the text in div
}
});
FYI: value attribute is not associated with div element, I guess you're referring to custom data* value attribute.
If so then you can change it using .data() method of jQuery. Example
$('div').data('value', 'whatever you wish');
JSFiddle
If I Understood your question correctly, You want to make sure that date picked is today or not, upon submission of form.
I've gone through google, ended up with an answer, Hope It work out.
function ValidRange(date1)
{
var date=new date(); //Today
//Convert these dates into milliseconds
var date1_ms=date1.getTime();
var date_ms=date.getTime();
//Calculate difference between them
var difference_ms = date_ms - date1_ms;
return difference_ms; // It will return 0 if date is today.
}
Note: You then need to parse the strings you are getting from the UI, with Date.parse, like this:
ValidRange(Date.parse(Date_Picked));
Try this.

Categories

Resources