parse time in dateBox(), or eliminate time in datebox - javascript

Is there a way to parse the time in dateBox(), or better yet eliminate it all together? I want someone to be able to use the dateBox() to choose the date only, but want to get rid of the time and use another function I create for the time.
thanks

one simple way of doing this is like this :
var date = new Date(e.parameter.start);// convert the string to a full date object (with time value) (the date widget is named 'start' in this example)
var dateOnly = date.setHours(0,0,0,0); // set time to '0' (hours,min,sec,millisec)or use the hours and minutes you get from elsewhere...
You can have a look at this example sheet where I use this to combine data from 2 columns (date and time) to create a complete date object

Related

Error converting selected date - Angular 8

I am making a filter between two dates, Start date and End date, the filter works perfect, it brings the data but it does not bring the complete data and it is because when selecting the dates and converting them to the format, it converts them but one day remains.
This way I am converting the dates:
FiltrarPorFechas(incial, final) {
this.ListaUsuarios = [];
const IniDate = new Date(incial);
const EndDate = new Date(final);
}
associate an image with debug and conversion:
As shown in the image, the initial and final dates arrive at the method thus "2019-07-10" and "2019-07-31" but when I try to convert them it puts them one day less as shown in the image.
I have tried to use moment formatDate and it does not work, I do not understand why and I do not want to add one day.
Somebody could help me ?
You may be potentially having an issue with Timezone. You could reset the time to 00:00:00 either using moment.js or from a normal Date() object.
Now, since your aim is to compare the times, you can use the diff() available from moment.js to achieve this. Please find the sample code below
(function() {
initial = '2019-07-09';
initial_formatted = moment(new Date(`${initial} 00:00:00`));
final = '2019-07-31';
final_formatterd = moment(new Date(`${final} 00:00:00`));
console.log(initial_formatted.diff(final_formatterd, 'days'));
// moment(new Date(`${incial} 00:00:00`)).format('YYYY-MM-DD HH:mm:ss');
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
More info
Moment diff()
Date Object set() method

Convert Time to Long format in AngularJS

I have an input box where I am displaying the time in the following format: HH:mm AM/PM.
<input type="text" ng-model="myTime" required/>
I am doing this using the date filter in AngularJS -
$scope.myTime = $filter("date")(new Date(), 'shortTime');
I want to pass this time to rest layer, which needs this time in the long format. Is there any way to do that in Angularjs?
PS- I would like to do it without using Moment.js, if possible.
Well, lets imagine, you have a string, containing "HH:mm" (btw, HH means that your hours range from 0-23, so ap/pm is kinda redundant there). To get your local date with that time into the long format you do:
// parse the time string
var time = "22:30".split(":")
// create a new date
var x = new Date();
// adjust time to target time
x.setHours(time[0]);
x.setMinutes(time[1]);
// finally, get the long version
x.getTime();

Moment.js comparing to times range with am pm

I have an dates array which has an array of appointment times in each date. I loop through a given date to get the appointment times like this :
var newdate = moment(appdate).format('DD/MM/YYYY'); //eg newdate is 04/04/2016
var newtime = moment(apptime).format('h:mm a');
appointmentArr[newdate].forEach(function(value, key) {
console.log(value); //these are all the appointment times for 04/04/2016 eg 11:22:00 AM
});
My question is, how can i check with moment.js, if "newtime" and "value" are within XX mins of each other? ive looked in the docs and on other posts but cant get it right with the AM PM formatting i am using.
To compare two dates with MomentJS you can use moment.diff, and you can specify the unit of measure you want your result in:
newDate.diff(oldDate, 'minutes');
You can then compare that value with your threshold and profit!
EDIT: But you should not format the date before comparing it, since format returns a string. Keep your moment object intact, and format it whenever you need to display it (or save the formatted string in another variable).

Having troubles with converting time in iso using java

I use the below code to format date time in iso format using java (I'm reducing 1 min from current time) and get the output as this "2016-03-17T11:38:21.xxxZ" < x represent some numbers> i want this to compare with the time which have mentioned in the DB.
Person who build that data insert query, he used javascript to get the time and format it in iso.
Date inside the DB is looks like this "2016-03-17T06:09:21.530Z" and its actual time is "11:39:21 GMT+0530 (India Standard Time)" which is similar to my current time but I'm comparing these two dates as string. and get 1min early data from DB.In that case i can't get an out put because as strings these two aren't match. can anybody recomand a solusion ?
I use OrientDB
Java Code
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Calendar date = Calendar.getInstance();
long t = date.getTimeInMillis();
date.setTimeInMillis(t);
date.set(Calendar.MINUTE, date.get(Calendar.MINUTE) - 1);
String time1minEarly = df.format(date.getTime());
Using Calendar.set() and Calendar.get() does not modify the date in a way you intend:
This will modify the minutes field in your case. So subtracting "1" will reduce the minute but not give a viable date for cases where minute initially is zero.
You may just subtract a minutes of milliseconds from your "t" variable to get a true minute offset.
And for ease of use you might also consider following advise from #Prashant and using LocalDateTime class from joda library.
Thanks Everybody for your support.
I figure out How to do this. it's pretty easy. Both #rpy and #Prashant are correct. Calendar is not suitable for solve my issue. Also LocalDateTime too. but it did help me to figure out the correct way.
#rpy and #Prashant they both did miss one thing that the javascript time represent the UTC time. that's the issue. (it's 5.5 hours behind compared to my location) so, I figure out this below code. it did what i wanted to do.
It's pretty easy. all you have to do is provide your zone id.
(You can get Zone id using this code : go to the link - http://www.javadb.com/list-possible-timezones-or-zoneids-in-java/)
Also you can choose so many formats by changing "DateTimeFormatter" value.
ZoneId UTCzoneId = ZoneId.of("UTC");
ZonedDateTime time1minEarly = ZonedDateTime.now(UTCzoneId).minusMinutes(1);
String UTCtime1minerly = time1minEarly.format(DateTimeFormatter.ISO_INSTANT);
Out put is similar to this : "2016-03-17T10:39:21.530Z"
(- UTC time at that time : 2016-03-17T10:40:21.530Z)

How to add three days to a given date in JavaScript?

I have one text box. When I enter a date with the format MM/DD/YYYY in that textbox and click outside the textbox I need to display the result as MM/DD/YYYY+3 using JavaScript.
For example, if my date is 12/31/2013 then the result would be 01/03/2014.
Hope this link will help you to find the answer:
adding-number-of-days-to-an-entered-date-in-javascript.
Have a look at the excellent library moment.js with which you easily can add three days to your original date.
In your case it would be something like:
var input = moment(myTextBoxValue);
input.add('d', 3); // input is now 3 days later
I guess the easiest to program and best readable solution would be to use a dedicated library for handling dates, such as Moment.js.
There, you have got the add function which allows you to add an arbitrary amount of time to a given point in time. E.g.:
moment().add('days', 7);
If you use the moment function to parse the time entered, use that as your source value, call add on it, and return it as JavaScript Date using the toDate function, you get exactly what you want.
So basically it comes down to:
var sourceDate = moment(new Date(2013, 7, 8)),
targetDate = sourceDate.add('days', 3),
result = targetDate.toDate();
Then, result contains the Date object you wanted to have.
I'd prefer that over native JavaScript handling of Date, as it is way more readble and hence understandable.

Categories