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"
Related
I have currently set up date formatting on my app..
Html
<span ng-bind="convertToDate(myDate) | date: 'medium'" id="dtText"></span>
Angular
$scope.myDate = new Date();
$scope.convertToDate = function (stringDate) {
var dateOut = new Date(stringDate);
dateOut.setDate(dateOut.getDate());
return dateOut;
};
I have the function working however it is displaying the time which i would like to remove. Just wondering what i would need to add to my function in order to prevent the time from displaying ?
How about just using
<span ng-bind="myDate | date: 'mediumDate'" id="dtText"></span>
No need to convert it.
The reason your convertDate function doesn't work is that Date.setDate only sets the date portion of the date value, leaving the time components intact. To reset the time components you would have to reset them individually something like
dateOut.setSeconds(0);
dateOut.setMinutes(0);
dateOut.setHours(0);
Specify format of date as argument
<span ng-bind="myDate | date: 'MMM d,y'" id="dtText"></span>
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");
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.
Having a small malfunction here.
When i receive a date input I can than transform it to whatever format I want (controller wise)
(e.g)
registerReq.dateFormat = $filter('date')(registerReq.dob, 'dd.MM.yyyy');
Works like a charm.
Now when I try to do the same thing on a Date() function
(e.g)
var timestamp = Date();
registerReq = $filter('date)(timestamp, 'dd.MM.yyyy, HH:mm');
It doesn't work.
I tried looking for information but i'm kinda lost.
Thanks for your answers! Fede.
Try to change Date() to new Date().
Hope it helps.
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