I am getting datetime as inputs using multiple timepickers in AngularJS. I want to add all the time inputs(dynamic) to give me a total duration.
e.g: if i input
Date {Wed Feb 03 2016 02:07:44 GMT+0530 (India Standard Time)}
Date {Wed Feb 03 2016 05:09:05 GMT+0530 (India Standard Time)}
It should return
Date {Wed Feb 03 2016 07:16:49 GMT+0530 (India Standard Time)}
Here is my code:
https://jsfiddle.net/nitishhardeniya/ytndyuck/
Using moment
var moment = require('moment');
var d1 = moment("Wed Feb 03 2016 02:07:44 GMT+0530", "ddd MMM DD YYYY HH:mm:ss Z");
var d2 = moment("Wed Feb 03 2016 05:09:05 GMT+0530", "ddd MMM DD YYYY HH:mm:ss Z");
var dur1 = moment.duration(d1.format("HH:mm:ss"));
var dur2 = moment.duration(d2.format("HH:mm:ss"));
var totalDur = dur1 + dur2;
var temp = d1.clone();
temp.startOf('day').add(totalDur);
console.log(temp.format()); // 2016-02-03T07:16:49+05:30
console.log(temp.format("ddd MMM DD YYYY HH:mm:ss Z")); // Wed Feb 03 2016 07:16:49 +05:30
var difference = date2 - date1;
var newDate = new Date(date2.getTime() + difference);
Try using Date.parse() method. It converts date to timestamp.
var time1 = new Date('2013-08-12 10:30');
var time2 = new Date('2013-08-12 12:30');
var time3 = new Date('2013-08-12 1:15');
var result = (Date.parse(time1) - Date.parse(time2)) + Date.parse(time3);
var resultTime = new Date(result);
alert(resultTime)
Fiddle
Related
Hey folks running into a problem with dates that I would love an extra set of eyes on. Any help would be appreciated.
Problem: I have a future event which will occur on a certain date, when I subtract the future event date from the current date I get a year that is in the past and an extra day.
let evenDate = "12 08 2019, 12:00 am";
let eventFormat = "MM DD YYYY, h:mm a";
const futureEvent = moment(evenDate, eventFormat).utc();
const now = moment().utc();
const timeDiff = moment(futureEvent - now).utc(); // also tried futureEvent.diff(now)
let daysLeft: moment(timeDiff).format('D') - 1; // have to subtract 1
console.log('futureEvent:', futureEvent);
// Sun Dec 08 2019 00:00:00 GMT-0700 (Mountain Standard Time)
console.log('now:', now);
// Wed Dec 04 2019 10:18:26 GMT-0700 (Mountain Standard Time)
console.log('timeDiff:', timeDiff);
// Sun Jan 04 1970 06:41:33 GMT-0700 (Mountain Standard Time)
let evenDate = "12 08 2019, 12:00 am";
let eventFormat = "MM DD YYYY, h:mm a";
const futureEvent = moment(evenDate, eventFormat);
const now = moment();
const timeDiff = moment.duration(futureEvent.diff(now));
let daysLeft = timeDiff.asDays();
const disp = document.getElementById("display");
disp.innerText = daysLeft.toFixed(); // 3.0123
console.log("futureEvent:", futureEvent.toString());
Same code i just get rid of .utc()s. used .duration() and for getting dates used .asDays().
Pen
I am displaying current day,month,date,year and time like this
Mon Oct 24 2016 17:09:25 GMT+0530 (India Standard Time)
but i need to display like this
Mon Oct 24 2016 17:09:25
my code in javascript:
var timestamp = new Date();
editor.insertHtml( 'The current date and time is: ' + timestamp.toString());
How can i do this please can anyone tell me how to do this.
Thank you
If you are open to add a library, you should use moment.js
console.log(moment().format('ddd MMM DD YYYY hh:mm:ss'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
If not, a small work around
var d = new Date().toString();
var index = d.lastIndexOf(':') +3
console.log(d.substring(0, index))
Note: moment approach is more preferred
var date = new Date();
var n = d.toLocaleString();
document.getElementById("demo").innerHTML = n;
This is work for me.
var timestamp = new Date();
console.log(
timestamp.toString().split('GMT')
)
// Mon Oct 25 2021 17:56:11 GMT+0530 (India Standard Time)`
the Output will be Mon Oct 25 2021 17:55:02
let today = new Date();
today = today.toString();
today = today.split('G')[0];
console.log(today);
I have date and time format datetimepicker. I would like to add number of days to the datetime
inputdattime will be picked from datetimepicker
input datetime = Friday, October 23rd 2015, 12:00:00 am
output should be if i add 2 days sunday, oct 25 2015 12:00:00 am
how to achieve this
var deal_from_date = new Date();
var numberOfDaysToAdd = 7;
var date1 = deal_from_date.setDate(deal_from_date.getDate() + numberOfDaysToAdd);
actual Output should be
"dddd, MMMM Do YYYY, h: mm:ss a"
Sun Oct 25 2015 12:00:00
current output is:
Sun Oct 25 2015 12:54:06
setDate() method can be used to add days to a date
var today=new Date('23/10/2015');
var in_a_day=new Date(today).setDate(today.getDate()+2);
Try this code :-
var d = new Date('oct 25 2015 12:00:00 pm');
d.setDate(d.getDate() + 2)
alert(d);
output is :- Tue Oct 27 2015 12:00:00 GMT+0530 (IST)
I am trying UTCString to above format. I can able to convert, problem is after conversion it shows a day before.
var newDate = this.getCellDate(target);
console.log(newDate); --> Dec 05 2014 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
cstDate = newDate.toISOString();
console.log(cstDate); -- > 2014-12-04 --- > **Expected --> 2014-12-05**
Use Date.UTC() method
var now = new Date(), // my date Thu Dec 04 2014 13:02:15 GMT+0300 (RTZ 2 (зима))
year = now.getFullYear(),
month = now.getMonth(),
day = now.getDay(),
hours = now.getHours(),
minutes = now.getMinutes(),
utcDate;
utcDate = new Date(Date.UTC(year, month, day, hours, minutes)); // Thu Dec 04 2014 16:02:00 GMT+0300 (RTZ 2 (зима))
Ext.Msg.alert('UTC Date', Ext.Date.format(utcDate, 'Y-m-d'));
Look at this "Thu Dec 04 2014 16:02:00" - i got utc time(+3 hours)
Fiddle example
Yeah i got the solution. I should not toISOString. instead i need to use toLocaleDateString
custdate = newDate.toLocaleDateString();
dueDate= custdate.split("/").reverse().join("-");
The code below mentioned is for comparision of date. Both date1 and mydate have similar values,But if i compare its not entering if loop. Any help appreciated
var date_arr = new Array( "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var Avl_date = document.getElementById("Available_Date").value;
var V_date1 = Avl_date.split('-');
var date1 = new Date (V_date1[2], date_arr.indexOf(V_date1[1]),V_date1[0]);
var myDate = new Date();
myDate.setHours(0,0,0);
//Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> date1
//Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> mydate
if(myDate.getTime() === date1.getTime())
{
//Not entering the loop
}
You're not setting the milliseconds of myDate to 0, so it keeps its original milliseconds. Use:
myDate.setHours(0,0,0,0);
Barmar is correct. setting the milliseconds will solve your problem.