Add time to the date value - javascript

I am using a jQuery plugin named mobiscroll to select a date, but the problem is that I also need to add to the result plus 15 minutes.
I have a function p(j), which returns 08/28/2012 12:15 - 12:15 (or only 08/28/2012 12:15 - as convenient), but instead i need 12:15 - 12:30. Are there any ideas?

According to the mobiscroll documentation setDate works with a Date object.
See this link on how to work with date objects in javascript. You don't need to do any string manipulations.
After you have the right date use the .scroller('setDate',newDate,true);

What about string manipulation?
var dateStr = p(j), //08/28/2012 12:15 - 12:15
timeStrSlice = dateStr.split(' ')[1].split(':'),
h = parseFloat(timeStrSlice[0]),
m = parseFloat(timeStrSlice[1]);
var nh = h,
nm = m + 15;
if(nm > 60) {
nh++;
nm = 0;
}
if(nh > 24) {
nh = 0;
}
var result = h + ":" + m + " " + nh + ":" + nm; // 12:15 12:30

Date d = new Date(2012,08,28);
d.setHours(12, 30, 0, 0);

See if this works for you:
var now = new Date();
//add 15 minutes to now
var out = new Date(now).setMinutes(now.getMinutes()+15)

Related

setHours() javascript returns a diffrent format then expected

I have this situation where I am in need of using the JavaScript
setTime()
So that I can modify the time, according to a number of seconds I have. For example I want to know what time it was 1400 sec ago.
I come to the conclusion that my cleanest and best solution it would be to use a combination of
getDate() and setHours() - setMinutes() - setSeconds() like in the example in this link:
https://codepen.io/Jaquelline/pen/rPgOKj?editors=1011
function myFunction() {
var myTime = new Array();
for(i=0; i <3599; i++){
var d = new Date();
var currentI = 3599-i;
myTime[i] = new Array();
myTime[i] = {
x: i,
y: d.setHours(d.getHours()-1) + ':' + d.setMinutes(d.getMinutes()) + ':'+ d.setSeconds(d.getSeconds()+currentI)
};
}
var s = JSON.stringify(myTime);
document.getElementById("cTimeArray").innerHTML =s;
var t = new Date();
t.setHours(t.getHours()-1);
t.setHours(t.getMinutes());
t.setHours(t.getSeconds()+1200);
document.getElementById("cTime").innerHTML =t;
}
The s variable returns something like this:
[{"x":0,"y":"1550821508351:1550821508351:1550825107351"},
{"x":1,"y":"1550821508351:1550821508351:1550825106351"},
While t returns :
Sun Apr 14 2019 08:45:08 GMT+0200 (Mitteleuropäische Sommerzeit)
If you simply want to know the time 1400 seconds ago, then why not just subtract 1400 seconds from the current time?
var past = new Date((new Date().getTime()) - 1400 * 1000);
getTime() returns the timestamp representing the date/time in milliseconds, hence the multiplication of 1400 by 1000.
You don't show the correct value.
Set d before and use the getters to show the value.
var myTime = new Array();
for (i = 0; i < 3599; i++) {
var d = new Date();
var currentI = 3599 - i;
myTime[i] = {};
d.setHours(d.getHours() - 1);
d.setMinutes(d.getMinutes());
d.setSeconds(d.getSeconds() + currentI);
myTime[i] = {
x: i,
y: d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds()
};
}
var s = JSON.stringify(myTime);
console.log(s);

Find difference between two dates with javascript [duplicate]

This question already has answers here:
Difference between dates in JavaScript
(8 answers)
Closed 8 years ago.
I am trying to find the difference between two dates. The dates are got with jquery and I am using datejs too. When using datejs it picks up my date as US thinking it is MM/DD/YYYY instead of dd-mm-yyyy. My result for difference is NaN. How do I work this out. Am I miles out or anywhere near close?
var msMinute = 60*1000,
msDay = 60*60*24*1000;
start = $('#reconcile_start_date').val(); // 10-12-2014 | dd-mm-yyyy
end = $('#reconcile_end_date').val(); // 15-12-2014 | dd-mm-yyyy
start = new Date(start);
end = new Date(end);
console.log(Math.floor((end - start) / msDay) + ' full days between ' + end + ' and ' + start);
difference = Math.floor((end - start) / msDay);
if(difference > 30){}
try this:
$(document).ready(function(){
var msMinute = 60*1000;
var msDay = 60*60*24*1000;
var start = '10-12-2014'; // October 12
var statarr=start.split('-');
var end = '12-15-2014'; // December 15
var endarr=end.split('-');
var dstart = new Date(statarr[0]+'/'+statarr[1]+'/'+statarr[2]).getTime();
var dend = new Date(endarr[0]+'/'+endarr[1]+'/'+endarr[2]).getTime();
var diff = parseInt(dend-dstart);
console.log(Math.floor(diff / msDay) + ' full days between ' + end + ' and ' + start);
difference = Math.floor((end - start) / msDay);
if(difference > 30){
}
});
// for UK formate use this:
var start = '12-10-2014'; // October 12
var statarr=start.split('-');
var end = '15-12-2014'; // December 15
var endarr=end.split('-');
var dstart = new Date(statarr[1]+'/'+statarr[0]+'/'+statarr[2]).getTime();
var dend = new Date(endarr[1]+'/'+endarr[0]+'/'+endarr[2]).getTime();
and rest is same.
The issue is around the parsing of the dates, by default JS wont parse the date in the format.
Some more examples on how to convert the date format from UK format can be found (Why does Date.parse give incorrect results?)
More information of the dateString param, formats and browser behaviour - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
See this sample. http://jsbin.com/fayerihipu/2/
$(document).ready(function(){
var msMinute = 60*1000,
var msDay = 60*60*24*1000;
var start = '10-12-2014'; // October 12
var end = '12-15-2014'; // December 15
var dstart = new Date(start).getTime();
var dend = new Date(end).getTime();
var diff = parseInt(dend-dstart);
console.log(Math.floor(diff / msDay) + ' full days between ' + end + ' and ' + start);
difference = Math.floor((end - start) / msDay);
if(difference > 30){
}
});
Officially, the only date format supported by JavaScript is a simplified version of ISO-8601: yyyy-mm-dd, and almost all browsers also support yyyy/mm/dd as well.
So you need to do something like this to parse your dates:
var parts = start.split('-');
start = new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10) - 1,
parseInt(parts[0], 10));
//Date uses zero-based month numbers, and so we have to subtract one from the month number
Take a look here for more details.

Javascript date returning NAN in IE8

I am trying to parse date like this 2012-12-07T16:18:15+05:30 which I am receiving from database in string format.
The parse function I am using is:
var jstime = new Date("2012-12-07T16:18:15+05:30");
var h = jstime.getHours();
var m = jstime.getMinutes();
var s = jstime.getSeconds();
var f = "am"
if(h >= 12)
{
f = "pm";
h = h - 12;
}
if(h == 0)
{
h = 12;
}
var str;
str = jstime.toDateString();
str = str +"," + h.toString() + ":" + m.toString() + ":" + s.toString() + " " + f.toString();
However,IE8 browser returning NAN at very first line i.e. jstime is NAN in IE8,while working fine in other browsers.
so, Is there any alternate way to parse date that works well in all browsers?
I need it accepts date in above format & returns date in format:
Fri Dec 07 2012,4:18:15 pm?
If you can be sure of the format, you can regex it:
var match = "2012-12-07T16:18:15+05:30".match(/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)([+-])(\d\d):(\d\d)/);
var jstime = new Date();
jstime.setUTCFullYear(parseInt(match[1],10));
jstime.setUTCMonth(parseInt(match[2],10)-1);
jstime.setUTCDate(parseInt(match[3],10));
jstime.setUTCHours(parseInt(match[4],10)-parseInt(match[7]+"1",10)*parseInt(match[8],10));
jstime.setUTCMinutes(parseInt(match[5],10)-parseInt(match[7]+"1",10)*parseInt(match[9],10));
jstime.setUTCSeconds(parseInt(match[6],10));
But if it's coming from the server-side, you may be able to format it more reliably there.
You can do this -
date = Date.parse("2012-12-07T16:18:15+05:30");
var jstime = new Date(date);
var h = jstime.getHours();
var m = jstime.getMinutes();
var s = jstime.getSeconds();
continue your code .....
I think this will help you.

Is there a reliable way to convert a naive UTC time stamp to local time with javascript?

Determining a user's timezone server side and converting from UTC has proven more trouble than its worth.
Is there a reliable way for javascript/jquery to determine the timezone of the user and apply the offset to a UTC datetime stamp (2012-08-25 10:59:56.511479) and output in my desired format (Aug 25 '12 - 10:59AM)?
What might the jquery code look like to say
// dom ready
$('span.localtime').each(function(e) {
// get stamp and apply conversion
});
.getTimezoneOffset() is available on the date object, and gives you the offset from UTC in minutes.
var offset = (new Date()).getTimezoneOffset();
// convert myUtcDate to a date in local time
myUtcDate.setMinutes(myUtcDate.getMinutes() + (offset*-1));
Thus:
$('.span.localtime').each(function() {
var myUtcDate = new Date($(this).html()); // assuming "2012-08-25 10:59:56.511479"
myUtcDate.setMinutes(myUtcDate.getMinutes() + (myUtcDate.getTimezoneOffset() * -1));
$(this).html(myUtcDate.toString());
});
Note that myUtcDate.toString() could be replaced with any date formatting you want. In your case, it might look like
$(this).html(formatDate(myUtcDate));
function formatDate(d) {
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var y = d.getFullYear().toString().slice(-2); // "12"
var m = months[d.getMonth()]; // "Aug"
var d = d.getDate(); // "25"
var ampm = 'AM';
var h = d.getHours();
if(h>=12) {
h -= 12;
ampm = 'PM';
}
if(h == 0)
h = 12;
var min = ("00" + d.getMinutes()).slice(-2);
return m + " " + d + " '" + y + " - " + h + ":" + min + ampm;
}
You might want to use a date format plugin for formatting dates in a neater more reliable manner.
Also, have a look at https://github.com/GregDThomas/jquery-localtime - it wraps all this up in a simple to use jQuery plugin.

Javascript: wrong date calculation

So I just have posted a question about this code (which was answered):
$(document).ready(Main);
function Main() {
ConfigDate();
}
function ConfigDate() {
var currentTime = new Date();
var dayofWeek = currentTime.getDay();
var daysSinceThursday = (dayofWeek + 3) % 7
var lastThursday = new Date(currentTime.getDate() - daysSinceThursday);
var dd = lastThursday.getDate();
var mm = lastThursday.getMonth() + 1;
var yyyy = lastThursday.getFullYear();
$("#last_thursday").text(yyyy + " / " + mm + " / " + dd);
}
The problem now is that the date that appears in my cell is 1969 / 12 / 31 (which isn't even a thursday).
Did I do something wrong while calculating last thursday date?
This is because .getDate() returns the day of the month. So you are building your date based on a serial number of something less than 30, which won't even set your seconds above 1.
Use .setDate() instead of building a new date:
date.setDate(date.getDate() - daysSinceThursday);
.setDate() will modify your existing date object, it doesn't return a new date.
You're trying to set a Date based only on the day of the month of the last Thursday. Try something like this:
var daysSinceThursday = (dayofWeek + 3) % 7;
var lastThursday = new Date(currentTime.getTime());
lastThursday.setDate(currentTime.getDate() - daysSinceThursday);
var dd = lastThursday.getDate();
var mm = lastThursday.getMonth() + 1;
var yyyy = lastThursday.getFullYear();
http://jsfiddle.net/rAuRF/3/

Categories

Resources