TimePicker return object not time angular 4 - forms

how can i make timepicker in form return the time i chose .
TimePicker in form :
<label class="padd"> Time of visiting </label>
<ngb-timepicker [(ngModel)]="meridianTime" [meridian]="meridian"
formControlName="time" id="time" ></ngb-timepicker>
TypeScript part:
defaultTime = {hour: 13, minute: 30};
customTime: NgbTimeStruct = {hour: 13, minute: 30, second: 0};
hourStep = 1;
minuteStep = 15;

You have time. Here is docs: link
You have time as hour, minute, second props. You can convert it to whatever you need. To convert you can use internal JavaScript API: Date or some libraries like moment. Or something like this: https://stackoverflow.com/a/27979128/2898694 :)
So you have date in timepicker format. As object I mean. You can write something like this:
prepareDate(dateAsObject) {
return dateAsObject.hour + ':' + dateAsObject.minute + ':' + dateAsObjet.second;
}
This is first thing in my head. Of course you can prepare date as you wish.

Related

Using Primeng Datepicker how do I only enable the last day of months?

So my question is pretty simple. I'd like to have specific dates that are enabled on a datepicker, like only the last day of any month, for example 30/06/2019, 31/07/2019
Primng Datepicker
<p-calendar formControlName="date"></p-calendar>
It is a simple hack for your requirement.
In your component.html use min and max date and (onMonthChange) method to update min and max value of date when user changes the month. Trick is just make min and max date equal to last day of the month.
<p-calendar [(ngModel)]="value" tabindex="0" [maxDate]="maxDateValue"
[minDate]="minDateValue" readonlyInput="true"
(onMonthChange)="onMonthChange($event)">
</p-calendar>
And in component.ts file use below code:
public maxDateValue: Date;
public minDateValue: Date;
ngOnInit() {
this.setMinMaxDate();
}
setMinMaxDate() {
var nowdate = new Date();
this.maxDateValue = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);
this.minDateValue = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);
}
// method to handle month change.
onMonthChange(e:any) {
this.minDateValue = new Date(e.year, e.month, 0);
this.maxDateValue = new Date(e.year, e.month, 0);
}
You must be thinking why at time of initialization I am using nowdate.getMonth() + 1, but inside onMonthChange only e.month, not adding + 1.
You need to go through this find last and first date of month article how to get the last and first date of a month and print the values of month in console and see the differences. Then you will understand easily.
If you just need one day of each month you should use the month view
<p-calendar [(ngModel)]="dateValue"
view="month" dateFormat="mm/yy"
[yearNavigator]="true"
yearRange="2000:2030"></p-calendar>

Why is Moment JS setting Milliseconds

I am trying to create a moment object from string which contains date in format DD/MM/YYYY. I want to set this to be DD/MM/YYYY 23:30. For this, I am writing the code as
moment.utc('30/04/2016','DD/MM/YYYY').hours(23).minutes(30)
However, that for some reason sets the milliseconds to 300. I am unable to set milliseconds to 0 even using the code below
moment.utc('30/04/2016','DD/MM/YYYY').hours(23).minutes(30).milliseconds(0)
I only need my date to be like 30/04/2016 23:30:00 and not like 30/04/2016 23:30:00:300
Please see JS Fiddle
Why is the millisecond part being set to 300 and if why am I not able to set it to 0?
Use this
moment.utc('30/04/2016 23:30:00 0','DD/MM/YYYY HH:mm:ss SSS')
See this http://momentjs.com/docs/#/parsing/string-format/ & http://momentjs.com/docs/#/displaying/
Try this,
<body>
<p>Click the button to display date</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
var n=moment.utc('30/04/2016','DD/MM/YYYY').hours(23).minutes(30).seconds(0).milliseconds(0).format('DD/MM/YYYY hh:mm:ss.SSS');
document.getElementById("demo").innerHTML = n;
}
</script>
</body>
working fiddler,
http://jsfiddle.net/fhdd8/356/
Well u can set time according to your choice by default .now() add upto millisecond.
var datetime = moment().set({ hour: 10, minute: 22, second : 19 , millisecond : 999 });
var datetime2 = moment().hour(23).minute(2).second(20);
var time3 = moment('23:11:22.322', "LTS.SSS", true);
datetime.format(LTS.SSS);
datetime2.format(LTS);
datetime.format("hh:mm:ss.SSS")
LTS - Localized formats
Time LT 8:30 PM
Time with seconds LTS 8:30:25 PM
SSS - Milisecond*s
For display use format()

I need to get number of days from between two dates

I need to get number of days from between two dates.
For example
2015.08.10 12:00 - 2015-08-12 12:00 result:2 days
i need take the result like that i did something but it doesn' work.
Template.RezSistemi.events({
"click #rezervasyonkaydet": function(event, template){
var date1_ms = template.$('#iadetarihi').val();
var date_ms=template.$('#alistarihi').val();
var formatli =date_ms-date1_ms;
alert(formatli);
}
});
As Kresten suggested, time handling is difficult without the help of a good library - and that goes for most programming languages. moment.js is a great library for javascript, which does just that.
Here is what you can do using that:
Template.RezSistemi.events({
"click #rezervasyonkaydet": function(event, template){
var date1_ms = moment(template.$('#iadetarihi').val(), "YYYY.MM.DD HH:mm");
var date_ms= moment(template.$('#alistarihi').val(), "YYYY.MM.DD HH:mm");
var days = moment.duration(date_ms - date1_ms).asDays();
alert(days + ' days');
}
});
Time handling is very hard, use a library!
MomentJS can do it:
http://momentjs.com/docs/#/displaying/to/
Falsehoods programmers believe about time:
http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time

Jquery onselect datepicker: if specific date then alert

This is my first question on stckoverflow so I hope I'm asking right.
On my website i'm using a jquery datepicker. This works fine. However, I need the following addon:
- When a user selects a date which is a specific date (var specificDate), alert with hello world.
This code already triggers the alert when selecting a date. However I am not able to figure out how to compare the "var date" with the array of specifc dates.
onSelect: function(dateText, inst){
var date = $(this).datepicker('getDate');
var specificDate = ["12-12-2013","11-12-2013"];
for (i=0; i < specificDate.length; i++){
if (date == specificDate [i]){
alert("hello world");
}
}
});
I think I need to know more about the date formats. My question is to get me a bit further with this how to make the comparison.
Thank you in advance!
Reference
if ($.datepicker.parseDate('dd-mm-yy', specificDate[0]) > $.datepicker.parseDate('dd-mm-yyyy', specificDate[1])){
alert(specificDate[0] + 'is later than ' + specificDate[1]);
}

How can I calculate the time between 2 Dates in typescript

This works in Javascript
new Date() - new Date("2013-02-20T12:01:04.753Z")
But in typescript I can't rest two new Dates
Date("2013-02-20T12:01:04.753Z")
Don't work because paremater not match date signature
Use the getTime method to get the time in total milliseconds since 1970-01-01, and subtract those:
var time = new Date().getTime() - new Date("2013-02-20T12:01:04.753Z").getTime();
This is how it should be done in typescript:
(new Date()).valueOf() - (new Date("2013-02-20T12:01:04.753Z")).valueOf()
Better readability:
var eventStartTime = new Date(event.startTime);
var eventEndTime = new Date(event.endTime);
var duration = eventEndTime.valueOf() - eventStartTime.valueOf();
In order to calculate the difference you have to put the + operator,
that way typescript converts the dates to numbers.
+new Date()- +new Date("2013-02-20T12:01:04.753Z")
From there you can make a formula to convert the difference to minutes or hours.
It doesn't work because Date - Date relies on exactly the kind of type coercion TypeScript is designed to prevent.
There is a workaround for this using the + prefix:
var t = Date.now() - +(new Date("2013-02-20T12:01:04.753Z"));
Or, if you prefer not to use Date.now():
var t = +(new Date()) - +(new Date("2013-02-20T12:01:04.753Z"));
See discussion here.
Or see Siddharth Singh's answer, below, for a more elegant solution using valueOf()
// TypeScript
const today = new Date();
const firstDayOfYear = new Date(today.getFullYear(), 0, 1);
// Explicitly convert Date to Number
const pastDaysOfYear = ( Number(today) - Number(firstDayOfYear) );
OP wanted a time as the result:
return new Date(this.submitted.valueOf() - this.created.valueOf())