Extracting only date from moment object - ionic-framework

I am using ion2 calendar for creating a multidatepicker. Here I am using onSelect event emitter.
onSelect($event) {
console.log("onSelect event called");
console.log($event);
};
Here when I print '$event' the console output looks like this:
How to extract only date from this moment object in the format 'DD-MMMM-YYYY'?

Below one liner will give you desired output:
let formattedDate = moment($event[0].Moment._d).format('DD-MMMM-YYYY');
So your function will become
onSelect($event) {
let formattedDate = moment($event[0].Moment._d).format('DD-MMMM-YYYY');
console.log("onSelect event called");
console.log($event);
};
Note : As I can not debug your code so please check first that if $event[0].Moment._d is giving you highlighted date (_d: Wed May 16 ........) in your question.

In your case just use moment($event[0]).format('DD-MMMM-YYYY')
console.log(moment().format('DD-MMMM-YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>

Related

Conditional formatting for dates

I'm trying to come up with a simple conditional format formula for highlighting cells that have a date that is greater than three months older than today's date. It seems though that the "Date is before" option only gives a few options, none of them seem to allow what I'm looking for. Is there a custom formula that could accomplish this?
Edit: attaching a snip of the column in question:
Formula :
=DAYS(now(),B2)>90
Go to the custom formula in the conditional formating rules and use this:
=DATEDIF(A1,TODAY(),"D")<90
try:
=1*C2>DATE(YEAR(TODAY()), MONTH(TODAY())+3, DAY(TODAY()))
also make sure you have valid dates and not plain text dates. you can test this with ISDATE formula
You can use Apps Script and a Custom Menu in order to solve your issue with setting the color in the cell depending on the date. Go to Tools->Script Editor and paste this code:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Check Difference', 'dateDifference')
.addToUi();
}
function dateDifference(){
var sheet = SpreadsheetApp.getActiveSheet().getActiveRange(); // Get the selected range on the sheet
var dates = sheet.getValues(); // Get the values in the selected range
var oneDay = 1000*60*60*24;
var row = 1;
var re = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/; // This will help you to check if it's really a date
dates.forEach(function(el){ // Iterate over each value
if(typeof el[0] == 'object'){ // Check if it's really a date
var gmtZone = el[0].toString().split(" ")[4].split(":")[0]; // Take the date's GMT
var dateFormatted = Utilities.formatDate(el[0], gmtZone, "dd/MM/yyyy"); // Format the date
if(re.test(dateFormatted)){ // Test if it's the right format
// This part will calculate the difference between the current date and the future date
var futureDateMs = new Date(el[0]);
var todayDateMs = (new Date()).getTime();
var differenceInMs = futureDateMs - todayDateMs;
var differenceInDays = Math.round(differenceInMs/oneDay);
if(differenceInDays >= 91.2501){ // Test if the difference it's greater to 91.2501 days (3 motnhs)
sheet.getCell(row, 1).setBackground("#00FF00"); // Set the color to the cell
}
row++;
}
}
});
Save it by clicking on File->Save.
Then you can select a range in a column and click on Custom Menu->Check Difference as you can see in the next image:
As you can see, you will get the desired result:
Notice
It's really important to be careful with what you consider to be a "month", I mean how many days you are going to take into consideration. In my code, I took Google's suggestion of 1 = 30.4167.
Docs
These are other Docs I read to be able to help you:
Utilities.formatDate()
Working with Dates and Times.
I hope this approach can help you.

Attempting to get the response from a TimeItem on my google form?

When I use getResponse(), it returns a string that doesn't even have the AM or PM value. Thus I cannot use the Date and Time functions on the response. Does someone know how to get my timeItem response to save properly.
I do know that I need to create a Date object in order to use the date and time functions. That's not the problem.
var tester = Responses[3].getResponseForItem(Items[7]).getResponse();
It returns at string like "3:00" when I would rather it return something like "3:00PM"
The TimeItem class is defined on the 24-hour clock. I don't think they explicitly provide AM and PM tags. Note, this is also the case with DateTimeItems.
Documenation: TimeItem
If you want the string to be 3:00PM/AM instead of 3:00, write a short conditional statement.
Something like:
if(timeItem > 12:00 AND timeItem < 23){
tester = tester + "PM";
} else {
tester = tester + "AM";
}

Add day to date with momentjs

I am trying to add a day to my date:
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate);
However, this keeps returning an obscure object {_i: "2017-12-20T21:06:21+00:00", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array(7), …}
fiddle:
http://jsfiddle.net/rLjQx/4982/
Does anyone know what I might be doing wrong?
You are logging a moment object. As the Internal Properties guide states:
To print out the value of a Moment, use .format(), .toString() or .toISOString().
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
Please note that you can get the current date using moment() (no need to use new Date()) or moment.utc().
I will go with this one, simple works for me and I don't think you need other function to only add day in moment.
var yourPreviousDate = new Date();
var yourExpectedDate = moment(yourPreviousDate).add(1, 'd')._d;
The add method modifies the moment object. So when you log it, you're not getting an obscure object, you're getting the moment object you're working with. Are you expecting a formatted date? Then use format or some other method.
I agree with other answers just providing shortcut and different ways
You can do the format at the same time
moment().add(1,'d').format('YYYY-MM-DD');
or you can just format any date or date object
moment(result.StartDate).format('YYYY-MM-DD');

SAPUI5 - Dateformat - How format a date with Dateformat

let's say I have a Date as a String, formated in yyyy-MM-dd, and I want it to be formated as style:"short".
I want just to use Dateformat.
I used this https://openui5.hana.ondemand.com/#docs/guide/91f2eba36f4d1014b6dd926db0e91070.html to get an idea of how to use DateFormat.
But I can't see, what's wrong with my code:
date: function(sdate) {
var regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
if (!sdate.match(regex))
return "no valid date given";
jQuery.sap.require("sap.ui.core.format.DateFormat");
var oDateFormat = sap.ui.core.format.DateFormat.getInstance({pattern: "yyyy-MM-dd", style: "short"});
return oDateFormat.format(sdate); //date should be returned here in "short"-style
}
The console tell's me
TypeError: j.getTime is not a function.
Also it seems like the WebIDE doesn't know a function Datetime.format().
Can you help?
You'd probably reread the documentation in your link: to convert String into JS Date, you have to use DateFormat.parse method.

How to return a normal javascript date object?

Is it possible to return a javascript date object from this? I have an text input field with the calendar and want to return a standard date object from it's value... Is this possible?
Is this what you are looking for?
var date = '2016-06-12'; // Can be document.getElementById('myField').value that points to your date field.
var date_parts = date.split('-');
var date_obj = new Date(date_parts[0],date_parts[1],date_parts[2]);
console.log(date_obj);
You can also simply use
new Date(document.getElementById('myField').value)
and see if it works. The date function is smart enough to parse based on browser's locale. This should work for time as well. Eg. new Date('2016-06-12 04:15:30')