How to add 4 hours to a formated date/time - javascript

I've used Date Format 1.2.3 to format my time into yyyy-mm-dd hh:MM:ss and used it for a startTime variable. How can add 4 hours to the startTime string and use it for a new variable (endTime)?

You can try to parse it as Date, add 4 hours and then format it into text again.
var d = new Date(startTime);
d.setHours(d.getHours() + 4);
var endTime = // Format d variable as you like

Parse it as a date then add 4 to the hours
Here's an example with the date object
var today = new Date();
today.setHours(today.getHours() + 4);
Javascript will automatically update the day/month/year if required

Related

Javascript date with xs:dateTime

Is there a simple way to convert standard JavaScript date format to XS:date Time
So I have a date value (new Date()) and I need in the format: 2021-06-04T13:36:00.000+05:00
It strange but could not find simple solution
I think this worked for you
var date = new Date();
var formattedDate = date.toISOString() + "+" +(date.getTimezoneOffset() / 60) + ":00"
console.log(formattedDate)

How to get utc date from timestamp using javascript?

I have a variable containing timestamp ( in seconds generated with php ), I used following code to create date object using javascript
var newdate = new Date(arrivalDate);
then used following code to get formated date in utc
var tempdate = newdate.getUTCFullYear()+'-'+('0' + (newdate.getUTCMonth()+1)).slice(-2)+'-'+('0' + newdate.getUTCDate()).slice(-2);
It returns me a 1 day before date, not sure how to get utc date from a timestamp.
var newdate = new Date(arrivalDate);
newdate.toUTCString();

Datetime diff in minutes

I have 2 DateTime field in a form, and I want the difference between these 2 fields in minute.
I tried to parse DateTime into Date but it's not working :
<script>
$(document).ready(function () {
$("#mybundle_evenement_button").click(function () {
var field1 = $("#mybundle_evenement_debut").val();
var field2 = $("#mybundle_evenement_fin").val();
var date1 = new Date(field1);
var date2 = new Date(field2);
alert(date1);
});
});
</script>
If I alert() date1, it shows Invalid Date.
But if I alert() field1, it shows 15/09/2017 13:32 (format is : days/months/year hour:minutes).
Is it possible that new Date(field1) isn't working because of the format ?
I know that if I succeed to parse DateTime into Date, it'll be easy to have the difference in minutes, but I don't understand why it says Invalid Date.
dd/MM/yyyy HH:mm isn't a valid date format for Date.parse()
You have to format your date to a valid Date Time String Format, for example:
var field1 = $("#mybundle_evenement_debut").val();
var ISODate1 = field1.replace(/(\d+)\/(\d+)\/(\d+)/, "$3-$2-$1")
var date1 = new Date(ISODate1);
alert(date1) // => Fri Sep 15 2017 13:32:00 ...
The problem is about the format you are getting the date from the field. new Date() don't accepts this format. I think the best is to parse the string yourself. If the format is always the same just use new Date(year, month, day, hours, minutes, seconds, milliseconds).
var day = field.splice(0,2); field.splice(0,1);
var month = field.splice(0,2); field.splice(0,1);
var year = field.splice(0,2); field.splice(0,1);
var hour = field.splice(0,2); field.splice(0,1);
var minute = field.splice(0,2);
It's depend on your browser. I'll suggest to use the standard format is '2013/12/09 10:00'.
Okay! come to the point. You need to manually format the date from my latest answer regarding this same kind of issue. Please take a look at this link : Stange javascript Date behaviour on particular dates
And you could try this below code for getting the date difference in minutes.
var startTime = new Date('2013/12/09 10:00');
var endTime = new Date('2014/12/09 10:00');
var difference = endTime.getTime() - startTime.getTime();
var result = Math.round(difference / 60000);
alert(result);

Convert date to other format in javascript

I am getting a date and a time from 2 input boxes with datebox plugin.
I have the date as "16-01-2015" and time as "11:37 AM". I need to send both to my server to add to the database but my database need the date in format: "2015-01-16 21:11:00"
How can I convert my both strings in a date or a string with the other format? I also need to convert 12 hour time to 24h time.
Using moment.js:
var dateInitial = "16-01-2015";
var timeInitial = "11:37 AM";
//Parse the date as String to Moment
var dateAsMoment = moment(dateInitial + " " + timeInitial, 'DD-MM-YYYY HH:mm A');
//Parse the date as Moment to String in the desired format
var dateToSend = dateAsMoment.format('YYYY-MM-DD HH:mm:ss')
http://jsfiddle.net/vcarvalho/w240pfz6/2/
// You can reverse the numbers in the date and adjust the hours as needed.
function parsedaytime(inputday, inputtime){
var dstr= inputday.value.split('-').reverse().join('-'),
tstr= inputtime.value.split(/:| /),
ampm= tstr.pop();
if(ampm== 'PM' && tstr[0]!== '12') tstr[0]-=-12;
return '"'+dstr+' '+tstr.join(':')+'"';
}
var inputday={value: "16-01-2015"},
inputtime={value: "11:37 PM"};
parsedaytime(inputday,inputtime)
returned value: (String)
"2015-01-16 23:37"

How to subtract two dates in javascript?

I want to get the difference between tow dates in javascript but my problem is that the first date is the currenet date of the PC :
today = new Date();
and the other date is a text format date like this for example:
other_date = '15.11.2013';
I want the today date to be same format as other_date and then subtract them , How can I change the format of the today to make the same as other_date ? and How I can make them both as date format to subtract them and get the difference correctly???
I would simply use the split function to get the date parts of the date string:
var today = new Date();
var other_date = '15.11.2013';
var dateparts = other_date.split('.');
var otherDate = new Date(dateparts[2], dateparts[1]-1, dateparts[0]); // substract 1 month because month starting with 0
var difference = today.getTime() - otherDate.getTime(); // difference in ms

Categories