Manipulating video timestamps in JavaScript - javascript

How can I do math using video timestamps of the format "00:01:00"?
For example, given a timestamp like, var start = "00:01:00";, how can I add time to start using another timestamp (e.g. var end = "00:05:00") and come away with a result using that same format (e.g. var sum = "00:06:00")?
I have looked into using Moment.js to do this, but that library wants to coerce everything to a Date, which doesn't seem like a good fit for what I'm trying to do.

Actually, you can do that with moment, check below code.
var date = moment("00:01:00", 'mm:ss:SS');
var date2 = moment("00:05:00", 'mm:ss:SS');
date.add(date2);
var element = document.getElementById('app');
element.innerHTML = date.format('mm:ss:SS');
<div id="app"> </app>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js" type="text/javascript"></script>

Related

How can I convert the value I get with datetimepicker to date and time?

var innerDate = document.getElementById('datetimepicker3').value;
The format I get from this line is: innerDate: "2021/07/07 14:00"
But I need to parse the date and time information here. How can I convert Start Date and Start Time into two separate variables?
I would recommend to parse that string with moments.js and then depending on your needs either get the individual parts for calculations or apply needed formatting
Example available at https://jsfiddle.net/Ly6qen3w/
let innerDateInput = "2021/07/07 14:00";
let innerDateMoment = moment(innerDateInput, "YYYY/MM/DD hh:mm");
console.log("Date:"+innerDateMoment.format("YYYY-MM-DD"));
console.log("Time:"+innerDateMoment.format("hh:mm"));
Try this:
var innerDate = new Date(document.getElementById('datetimepicker3').value)
Or you can attach an event handler to get the date whenever it changes
<script type="text/javascript">
$("#datetimepicker3").on("dp.change", function (e) {
let date = e.date;
console.log(date);
// no need to convert to a date
});
</script>
You can simple get that in different variable using various methods that are provided by JavaScript to get everything.
You can do is(id it's a date object else you have to convert it to date object)
let startDate = new Date(innerDate).getDate() + "/" + new Date(innerDate).getMonth() + "/" + new Date(innerDate).getFullYear();
let startTime = new Date("2021/07/07 14:00").getHours() +":"+ new Date("2021/07/07 14:00").getMinutes()
If you want something else you can retrieve that too with methods such as milliseconds,UFC time etc.
If you find this messy I would suggest you to use moment.js.
https://momentjs.com/

How to use moment.js properly?

I have made a sample page which contains this:
var date = '2015-04-03';
var formate = 'LLLL';
var result = moment(date).format(format);
console.log(result);
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
And as you can see I have included the moment.js file in order to retrieve data from it and displaying a valid date in the browser. (Here is the link to moment.js that I have inlcuded: link)
But I don't know why it does not work at all! Can u guys help how to use moment.js in proper way ?! Thanks...
You wrote "formate" instead of "format".
Appart from this, you code works fine.
var date = '2015-04-03';
var format = 'LLLL';
var result = moment(date).format(format);
document.write(result);
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>

Add one day to User-Input Day using Javascript in a PDF

I am needing to generate a field with the next date after a user inputs one date in a PDF using Javascript. Here is my code so far:
var numDaysToAdd = 1;
var inputDateString = getElementById("Date.1").value;
var resultDate = stringToDate(inputDateString);
resultDate.setDate( resultDate.getDate()+numDaysToAdd );
var result = dateToString( resultDate );
event.value = result;
Using this code, I get no return in the field. If I input "11.23.15" instead of the get Element for inputDateString, I get the result "12.11.16". So I have two issues - it doesn't seem to be pulling the value from Date.1, and when I add one day, it adds a whole lot more than a day. Thanks for the help.
You may be aware that Acrobat JavaScript (which is used within PDF) has a completely different object model than webbrowser JavaScript. It is therefore highly recommended to get the Acrobat JavaScript documentation, which is part of the Acrobat SDK documentation, downloadable from the developer section of the Adobe website.
That said, you would add the following to the Calculate event of the field where the result should appear (we assume that the date in the field "Date.1" has the format "MM/DD/YYYY"):
var numDaysToAdd = 1 ;
var fromDate = util.scand("mm/dd/yyyy", this.getField("Date.1").value) ;
var toDate = fromDate ;
toDate.setDate(fromDate.getDate() + numDaysToAdd) ;
event.value = util.printd("mm/dd/yyyy", toDate) ;
And that should do it.

How can I get date and time separately using jQuery?

In my table date is listed as "2015-07-31 06:02:20". How can I get date and time separately using jQuery?
I used some code but it shows some errors.
var timestamp = new Date(data.responsecusthistory.created_at);
var date = timestamp.toDateString();
Error: Invalid Date
var date = data.responsecusthistory.created_at.split(" ")[0];
var time = data.responsecusthistory.created_at.split(" ")[1];
If you want to have a time string (i.e. HH:MM:SS), try e.g. this:
var timestampTime = timestamp.toTimeString().split(' ')[0];
Anyway, there's no obvious reason why you get the "Error: Invalid Date", as long as
data.responsecusthistory.created_at
is a correct value, such as "2015-07-31 06:02:20". Please consider double-checking this variable.
Try this:
dateString= "2015-07-31 06:02:20";
temp = new Date(dateString);
dateStr= $.datepicker.formatDate('mm/dd/yy', temp );
for getting different formats check the link https://github.com/phstc/jquery-dateFormat.
Using different formats we will get different date, time etc in which ever forms we need it.

Full Calendar Get current date

I've been learning Ruby over the last year and I'm very new to JS so I'll try to explain this as best I can.
I am using Adam Shaw's full calendar plugin. All I want to do is get the current month I am viewing (and use that to limit how far in the future or past a user can navigate, but that's not the problem).
I can get the current date, sort of. But, because of my lack of JS knowledge I'm not sure how to access the date.
Here is the relevant section of the config file,
viewRender: function(view){
var maxDate = "<%= finish.strftime('%Y/%m/%d') %>";
var currentDate = $('#calendar').fullCalendar('getDate');
console.log(currentDate);
if (view.start > maxDate){
header.disableButton('prev');
}
}
When I inspect the console log I see this being output as I click through the months.
So as you can see it is displaying the current date in view. My question is how do I access the _d bit of the Moment variable so I can use it?
My understanding would be that the Moment is class instance and the stuff in the dropdown is like its attributes, would this be a correct interpretation?
To get the current date of the calendar, do:
var tglCurrent = $('#YourCalendar').fullCalendar('getDate');
This will return the date as a moment object. You can then format it as a string in the usual date format like so:
var tgl=moment(tglCurrent).format('YYYY-MM-DD');
For the actual time, use the format: YYYY-MM-DD LTS
FullCalendar's getDate returns a moment object, so you need moment's toDate() method to get date out of it.
So, in you code try:
console.log(currentDate.toDate());
and that should return a date object.
var moment = $('#YourCalendar').fullCalendar('getDate');
var calDate = moment.format('DD.MM.YYYY HH:mm'); //Here you can format your Date

Categories

Resources