Can I pass a piped date as an argument? - Angular - html

Is it possible to pass a piped date as an argument for a function?
I am thinking of something along the lines of...
A variable date: string that will be assigned a value passed from a datepicker
The date picker currently outputs date values in the format:
Fri Feb 09 2018 00:00:00 GMT+0000 (GMT Standard Time)
It is possible to display the date in binding with use of date | date:'yyyy-MM-dd'
I am wanting to pass into a function, that piped date - something like:
someFunction(date | date:'yyyy-MM-dd');
Is there any way that this could be possible?
Or will I have to pass the original long date into a function, mutate it and then use it?

I have since found a way to just JS it - How do I get a date in YYYY-MM-DD format?
This splits the date and then recombines it into a string:
onSelect(theDate: Date): void {
var yyyy = theDate.getFullYear().toString();
var mm = (theDate.getMonth()+1).toString();
var dd = theDate.getDate().toString();
var mmChars = mm.split('');
var ddChars = dd.split('');
var dateString = yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]);
this.meetingService.getMeetingsByDate(dateString);
}

Related

How to format abd get empty time with it with moment when it doesn't exist in string?

I have a use case where I have variable data in the following formats. When the string doesn't have time, the format object formats it with 12:00:00 => 12:00 am.
Well technically, the constructor that passes only a date IS instantiating it with a time, 12:00:00, even though it is implicit.
Example
'2021-02-12'
'2021-02-12T6:00'
'2021-02-19T18:00'
Code
const date = moment("2021-02-12");
date.format("h:mm a"); // 12:00 am
Expected
I'd like to know if that is possible that moment format the time with the empty string "" when time is not available in the data. e.g.
const date = moment("2021-02-12");
date.format("h:mm a"); // ""

Google Script taking date format YYYY-MM-DD HH:MM:SS as text, need to change timezone

I have a response which is returning the dates as YYYY-MM-DD HH:MM:SS format. However, due to timezones issues, I am trying to convert these in to GMT format.
I tried directly converting them but Google script always takes these as Text and thus returns Jan 1, 1970 as the output when using a Date object.
I tried adding the timezone offset to the string and then converting, but to no avail.
Is there any way I can convert this format into a proper date or directly convert it into GMT?
dateValue = '2019-02-20 18:30:00'
var tempDate = dateValue + ' +0530';
dateValue = new Date(tempDate)
I'd expect the outcome to be returned as a date, which I can then convert to GMT format
How about this?
function splitItUp() {
var dts="2019-02-18 19:52:14";
var tA=dts.split(' ');
var tD=tA[0].split('-');
var tT=tA[1].split(':');
var dt=new Date(Number(tD[0]),Number(tD[1])-1,Number(tD[2]),Number(tT[0]),Number(tT[1]),Number(tT[2]))
Logger.log(dt);
}

Ionic 2 JSON dates and moment.js trouble

I have a problem with dates manipulation with Ionic and moment.js.
I store some dates from a ion-datetime component :
{"year":2017,"month":7,"day":26,"hour":null,"minute":null,"second":null,"millisecond":null,"tzOffset":0}
And use moment.js to "humanize" date display :
let somedate = moment(some.date);
console.log('JSON date : ' + JSON.stringify(some.date) + ' resolved as : ' + somedate.format('DD/MM/YYYY'));
And got result :
Original JSON date : {"year":2017,"month":7,"day":26,"hour":null,"minute":null,"second":null,"millisecond":null,"tzOffset":0} resolved as : 26/08/2017
As you can consider, there's on month offset between original JSON date and moment display date...
What i'm missing ?
The Javascript month are in range of 0-11 i.e. January is 0, February is 1 and likewise, therefore it is moving you to the next month. Try subtracting 1 from month value, to get the correct month.
var some = {'date':{"year":2017,"month":6,"day":26,"hour":null,"minute":null,"second":null,"millisecond":null,"tzOffset":0} };
let somedate = moment(some.date);
console.log('JSON date : ' + JSON.stringify(some.date) + ' resolved as : ' + somedate.format('DD/MM/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Finally, with your answers, i resolved the problem with a conversion method that take the original JSON date format and return a Javascript Date object, using TypeScript :
private _JSONDateToDate(jsonDate: any){
if(typeof jsonDate == 'object')
return new Date(jsonDate.year, (parseInt(jsonDate.month) - 1), jsonDate.day);
return jsonDate;
}
I substract 1 month of the "month" property in order to have a correct Date object... then, i can manipulate it with moment.js

parseInt() sometimes returning NaN

I'm trying to parse a DateItem response from a form so that it fits the form "M/D/YYYY" (ie 2014-08-05 would translate to 8/5/2014). Since the ItemResponse is of type String, I wrote the following to manipulate it:
s = s.replace(/(\d{0,4})\-(\d{0,2})\-(\d{0,4})/,'$2/$3/$1');
var arr = s.split('/');
return parseInt(arr[0]) + '/' + parseInt(arr[1]) + '/' + parseInt(arr[2]);
But parseInt(arr[0]) just randomly started returning NaN a couple days ago. arr[0] is not null when it comes time to parse, and it's of type string. Only that value in the array has the problem. I parseInt to remove the preceding 0 on single digits. Any thoughts?
Why does parseInt return NAN for "08" string and return 7 for "07" string?
Basically, a leading 0 indicates that it is a number in octal. 8 is not a valid octal digit, hence NaN. Use parseint(arr[0], 10) to specify the base. You'll probably want to do the same for arr[1].
You may want to use Utilities.formatDate(), this will return a formatted date string in a format of your choosing.
For your example:
var formattedDate = Utilities.formatDate(new Date(formDataDateValue), Session.getScriptTimeZone(), "M/d/yyyy");

DateField: selectedDate shows hours as 24:00:00

With a DateField component, the selectedDate.getHours returns as 24:00:00. I want it to return as 00:00:00.
Is there an easy way to do this?
Thanks!
UPDATE:
First, I set a variable in my Model that equals the selectedDate of a DateField component:
model.generalInfo.endDate = endDate_df.selectedDate;
Then I set another variable based on that value and I trace it out:
param.todate = df.format( model.generalInfo.endDate.toString() );
And this is where I see the time equal to 24:00:00
you could try something like
selectedDate.time = selectedDate.time - 24 * 60 * 60 * 60 * 1000
as a Date.time represents miliseconds since 1970 or whatever.. you substract 24 hours..
if it not works for you, you can create a new function or getter that converts it, or you can create a new mxml module, with DateField as superclass, and you can override the getHours method. tons of options to do this..
It looks like you are using the Flex DateFormatter to format the Date object. Have a look at the docs for this class, it has a formatString property that you can use to control how to output the date (or in this case the time).
If you give the DateFormatter a format string that contains "H" will output the hour in 24 hour format using the number range 1-24. If the format string contains "J" it will output the hour in 24 hour format using the range 0-23.
To get your desired output, use "JJ" in the format string, in addition to any other items. For example to output the hours, minutes, seconds:
var someDate:Date = new Date(2012, 11, 5);
var df:DateFormatter = new DateFormatter();
df.formatString = "JJ:NN:SS";
var formatted:String = df.format(someDate); // 00:00:00
Also, as #Flextras mentioned, there is Flash localization API you can use which has the added benefit of converting date/time strings to the values used by their locale. These are the DateTimeFormatter classes:
fl.globalization.DateTimeFormatter
spark.formatters.DateTimeFormatter (Flex 4)
These classes format dates into the user's locale (or one that you specifcy), and format Date objects the same way the DateFormatter does.