removing time from angular date function - javascript

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>

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"

Angular-UI Bootstrap Datepicker new date

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.

How to use preciseDiff from readable-range.js Plugin

I am using Dynamic CRM 2013 and need to calculate date difference between 2 dates. I added to the form moment.js and readable-range.js.
All functions in moment.js are working fine. When it comes to preciseDiff from readable-range.js and use:
var bDt = new moment("2/22/2009");
var eDt = new moment("2/29/2016");
var dtDiff = moment.preciseDiff(bDt, eDt);
I am getting the following error:
Object doesn't support property or method 'preciseDiff'
Please advise.
Don't use the new operator with moment.
Also, if you are passing values in that format, you should provide a format string, otherwise values like 1/2/2014 might be interpreted as Jan 2nd in some regions, and Feb 1st in others.
Other than that, there's nothing wrong with your code.
var bDt = moment("2/22/2009", "M/DD/YYYY");
var eDt = moment("2/29/2016", "M/DD/YYYY");
var dtDiff = moment.preciseDiff(bDt, eDt);
Working jsFiddle here

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