I need add days in dates inside an array, like the image below:
When select show Semanal (weekly), add 7 days on each date, but my code is adding the same day on all subsequent dates:
var number_installments = 10, frequency = 1;
for(let i = 1; i <= number_installments; i++){
// console.log('Frequencia: ' + frequency)
let data = new Date();
let dia = data.getDate(), mes = data.getMonth(), ano = data.getFullYear(), zero = '';
if((mes + i) <= 9){
zero = '0';
}
let due_date = dia + '/' + zero + (mes + i) + '/' + ano;;
//console.log(i)
if(frequency == 1 && i != 1){
dia = data.getDate() + 7;
due_date = dia + '/' + zero + (mes + 1) + '/' + ano;
}
console.log(due_date);
}
This is because you have initialised your "data" object in every iteration which will only give the current date and day. Which after adding 7 results the same.
May be that comes from data externally.
let data = new Date();
Instead of getting date like that you can use,
var today = date.toJSON().slice(0,10)
which would result in the full date. It would make your code simpler to read and change.
I managed to solve with the help of Abhay H Soningra
var number_installments = 10, frequency = 1;
let data = new Date();
let dia = data.getDate(), mes = data.getMonth(), ano = data.getFullYear(), zero = '0', max_days, max_months = 12;
if(dia % 2 == 0){
max_days = 31;
} else{
max_days = 30;
}
for(let i = 1; i <= number_installments; i++){
// console.log('Frequencia: ' + frequency)
if((mes + i) <= 9){
zero = '0';
}
let due_date = dia + '/' + zero + (mes + i) + '/' + ano;;
//console.log(i)
if(frequency == 1 && i != 1){
dia = dia + 7;
if(dia > max_days){
mes = mes + 1;
dia = data.getDate();
}
if((mes + 1) > max_months){
mes = data.getMonth();
ano = ano + 1;
}
due_date = dia + '/' + zero + (mes + 1) + '/' + ano;
}
console.log(due_date);
}
Related
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I wrote the following code for displaying a different image depending on the date (right now this example just console logs a message). The code works fine in Chrome and Firefox on Mac, but does not work correctly or give any errors on Safari (in Safari the message does not change depending on the date, it just says the same). How is Safari processing this differently? How can I get this to work on Safari with minimal changes?
Here's a working repl.
Here's the code:
/* change these dates */
var ddt = new Date("2019, 8, 22");
var pre = new Date("2019, 8, 23");
var ton = new Date("2019, 8, 26");
var post = new Date("2019, 8, 27");
// todays date
var currDate = new Date();
var mm = currDate.getMonth() + 1;
var dd = currDate.getDate();
var yyyy = currDate.getFullYear();
// Get the date parts
var ddtDay = ddt.getDate();
var ddtMonth = ddt.getMonth() + 1;
var ddtYear = ddt.getFullYear();
//console.log(ddtYear, ddtMonth, ddtDay);
var preDay = pre.getDate();
var preMonth = pre.getMonth() + 1;
var preYear = pre.getFullYear();
//console.log(preYear, preMonth, preDay);
var tonDay = ton.getDate();
var tonMonth = ton.getMonth() + 1;
var tonYear = ton.getFullYear();
//console.log(tonYear, tonMonth, tonDay);
var postDay = post.getDate();
var postMonth = post.getMonth() + 1;
var postYear = post.getFullYear();
//console.log(postYear, postMonth, postDay);
// format the date parts
if (ddtDay < 10) {
ddtDay = '0' + ddtDay;
}
if (ddtMonth < 10) {
ddtMonth = '0' + ddtMonth;
}
if (preDay < 10) {
preDay = '0' + preDay;
}
if (preMonth < 10) {
preMonth = '0' + preMonth;
}
if (tonDay < 10) {
tonDay = '0' + tonDay;
}
if (tonMonth < 10) {
tonMonth = '0' + tonMonth;
}
if (postDay < 10) {
postDay = '0' + postDay;
}
if (tonMonth < 10) {
postMonth = '0' + postMonth;
}
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var ddtF = (ddtYear + '-' + ddtMonth + '-' + ddtDay);
var preF = (preYear + '-' + preMonth + '-' + preDay);
var tonF = (tonYear + '-' + tonMonth + '-' + tonDay);
var postF = (postYear + '-' + postMonth + '-' + postDay);
var today = (yyyy + '-' + mm + '-' + dd);
console.log(ddtF);
console.log(preF);
console.log(tonF);
console.log(postF);
console.log(today);
// logic
if (today >= postF) {
console.log('post');
} else if (today === tonF) {
console.log('ton');
} else if (today < tonF && today >= preF) {
console.log('pre');
} else if (today <= ddtF) {
console.log('ddt');
}
"2019, 8, 22" is not a portable date format. The Date constructor has a portable calling sequence where you give each component of the date as a separate argument, so use
var ddt = new Date(2019, 7, 22);
and similarly for all the other variables.
And remember that months are counted from 0 in JavaScript, so you need to subtract 1 from the month argument (August is 7).
/* change these dates */
var ddt = new Date(2019, 7, 22);
var pre = new Date(2019, 7, 23);
var ton = new Date(2019, 7, 26);
var post = new Date(2019, 7, 27);
// todays date
var currDate = new Date();
var mm = currDate.getMonth() + 1;
var dd = currDate.getDate();
var yyyy = currDate.getFullYear();
// Get the date parts
var ddtDay = ddt.getDate();
var ddtMonth = ddt.getMonth() + 1;
var ddtYear = ddt.getFullYear();
//console.log(ddtYear, ddtMonth, ddtDay);
var preDay = pre.getDate();
var preMonth = pre.getMonth() + 1;
var preYear = pre.getFullYear();
//console.log(preYear, preMonth, preDay);
var tonDay = ton.getDate();
var tonMonth = ton.getMonth() + 1;
var tonYear = ton.getFullYear();
//console.log(tonYear, tonMonth, tonDay);
var postDay = post.getDate();
var postMonth = post.getMonth() + 1;
var postYear = post.getFullYear();
//console.log(postYear, postMonth, postDay);
// format the date parts
if (ddtDay < 10) {
ddtDay = '0' + ddtDay;
}
if (ddtMonth < 10) {
ddtMonth = '0' + ddtMonth;
}
if (preDay < 10) {
preDay = '0' + preDay;
}
if (preMonth < 10) {
preMonth = '0' + preMonth;
}
if (tonDay < 10) {
tonDay = '0' + tonDay;
}
if (tonMonth < 10) {
tonMonth = '0' + tonMonth;
}
if (postDay < 10) {
postDay = '0' + postDay;
}
if (tonMonth < 10) {
postMonth = '0' + postMonth;
}
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var ddtF = (ddtYear + '-' + ddtMonth + '-' + ddtDay);
var preF = (preYear + '-' + preMonth + '-' + preDay);
var tonF = (tonYear + '-' + tonMonth + '-' + tonDay);
var postF = (postYear + '-' + postMonth + '-' + postDay);
var today = (yyyy + '-' + mm + '-' + dd);
console.log(ddtF);
console.log(preF);
console.log(tonF);
console.log(postF);
console.log(today);
// logic
if (today >= postF) {
console.log('post');
} else if (today === tonF) {
console.log('ton');
} else if (today < tonF && today >= preF) {
console.log('pre');
} else if (today <= ddtF) {
console.log('ddt');
}
i want to display TravelTimeHoursDiff and TravelTimeMinutesDiff in double digit now my time is shown as 7:0 i want to display like 07:00
if ($scope.DispatchStatus.ArrivalTime != undefined){
var today = $rootScope.getSysDate().split(" ");
var timeArrival = new Date(today[0] + ' ' + $scope.DispatchStatus.ArrivalTime);
var TravelTime = new Date(today[0] + ' ' + $scope.Route.TravelTime);
var timeArrivalHours = timeArrival.getHours();
var TravelTimeHoursDiff = timeArrivalHours - TravelTime.getHours() ;
var TravelTimeMinutesDiff = (timeArrival.getMinutes() - TravelTime.getMinutes());
if(TravelTimeHoursDiff < 0 || (TravelTimeHoursDiff <= 0 && TravelTimeMinutesDiff < 0) || (TravelTimeHoursDiff == 0 && TravelTimeMinutesDiff == 0)){
$scope.formvalidationbit = $scope.DispatchStatusAddForm[fieldName].$invalid = true;
angular.element('#' + fieldName).addClass('ng-invalid');
angular.element('#' + fieldName).removeClass('ng-valid');
$scope.DispatchStatusAddForm.$valid = false;
var errorbit = 1;
}else{
if (isNaN(TravelTimeHoursDiff)) {
TravelTimeHoursDiff = '--';
}
if (isNaN(TravelTimeMinutesDiff)) {
TravelTimeMinutesDiff = '--';
}
if(TravelTimeMinutesDiff <0){
TravelTimeMinutesDiff = TravelTimeMinutesDiff * (-1);
}
$scope.TravelTime = TravelTimeHoursDiff + ':' + TravelTimeMinutesDiff;
}
}
Just add leading 0 to values smaller then 10, something like:
let addLeadingZero(v){
return v < 10 ? ("0" + v) : v;
}
$scope.TravelTime = addLeadingZero(TravelTimeHoursDiff) + ':' + addLeadingZero(TravelTimeMinutesDiff);
i need to quit the zero of the value of mes1 mes2 and mes3 if the result is 010, 011 or 012
but when i use an if and run an alert of the new var the result is undefined
this is the function:
function getImpuestos() {
$.getJSON("https://soa.afip.gob.ar/av/v1/vencimientos/"+$("#cuit").val()+"", function(result){
for (var i = 0; i < result.data.length; i++) {
var fecha = result.data[i].vencimiento;
var periodo = fecha.substr(0,7);
var dt = new Date();
var year = dt.getFullYear();
var mes1 = year + "-0" + (dt.getMonth()+1);
var mes2 = year + "-0" + (dt.getMonth()+2);
var mes3 = year + "-0" + (dt.getMonth()+3);
if(mes1 > 7){
var mes1 = year + "-" + (dt.getMonth()+1);
}else if(mes2 > 7){
var mes2 = year + "-" + (dt.getMonth()+2);
}else if(mes3 > 7){
var mes3 = year + "-" + (dt.getMonth()+3);
}
if(periodo == mes1 || periodo == mes2 || periodo == mes3) {
$(".fa-spinner").css("display", "block");
$("#textobusqueda").css("display", "block");
console.log(result.data[i].idImpuesto);
alert(mes5);
buscarImpuesto(result.data[i].idImpuesto, result.data[i].vencimiento, result.data[i].tipoOperacion, periodo);
}
}
});
}
<label style="color:black" class="control-label">Ingresar CUIT Empresa</label>
<input id="cuit" type="text" class="form-control" onkeyup="searchCliente();">
You can use string.padStart instead of ifs:
var mes1 = year + "-" + (dt.getMonth()+1).padStart(2, "0");
var mes2 = year + "-" + (dt.getMonth()+2).padStart(2, "0");
var mes3 = year + "-" + (dt.getMonth()+3).padStart(2, "0");
And of course change mes5 in alert to something different.
I am making a simple time calculator in javascript. I have converted the times into 12-hour instead of 24 hour time for simplicity, however the code I have for calculating am/pm always shows am. Any reason why this would be happening?
Here is my code:
function solveTime(x) {
var suffixSolve = (utcHours + x) % 24;
var suffix = "am";
if (utcHours > 12) {
var suffix = "pm";
}
if (utcMinutes == 0) {
utcMinutesLead = "00";
}
if (utcMinutes < 10) {
utcMinutesLead = "0" + utcMinutes;
}
var timeSolve = (((utcHours + x) + 11) % 12 + 1);
var timeTotal = timeSolve + ":" + utcMinutesLead + " " + suffix;
var utcMod = x;
if (utcMod > 0) {
utcMod = "+" + utcMod;
}
document.getElementById(x).innerHTML = "(UTC" + utcMod + ") " + timeTotal;
}
and here is the code behind utcHours
var masterTimeUTC = new Date();
var utcHours = masterTimeUTC.getUTCHours();
var utcMinutes = masterTimeUTC.getUTCMinutes();
var utcSeconds = masterTimeUTC.getUTCSeconds();
var utcMinutesLead = masterTimeUTC.getUTCMinutes();
Example here: http://codepen.io/markgamb/pen/gwGkbo
The issue is you should be checking whether suffixSolve is greater than 12 instead of utcHours, because utcHours does not change due to the value of x. Since you can shift the hours forward and backwards, I created a variable shift to handle that.
function solveTime(x) {
if (x < 0) {
var shift = 24 + x;
} else {
var shift = x;
}
var suffixSolve = (utcHours + shift) % 24;
var suffix = "am";
if (suffixSolve > 12) {
suffix = "pm";
}
if (utcMinutes == 0) {
utcMinutesLead = "00";
}
if (utcMinutes < 10) {
utcMinutesLead = "0" + utcMinutes;
}
var timeSolve = (((utcHours + x) + 11) % 12 + 1);
var timeTotal = timeSolve + ":" + utcMinutesLead + " " + suffix;
var utcMod = x;
if (utcMod > 0) {
utcMod = "+" + utcMod;
}
document.getElementById(x).innerHTML = "(UTC" + utcMod + ") " + timeTotal;
}
var masterTimeUTC = new Date();
var utcHours = masterTimeUTC.getUTCHours();
var utcMinutes = masterTimeUTC.getUTCMinutes();
var utcSeconds = masterTimeUTC.getUTCSeconds();
var utcMinutesLead = masterTimeUTC.getUTCMinutes();
solveTime(4);
solveTime(0);
solveTime(-8);
<div id="4"></div>
<div id="-8"></div>
<div id="0"></div>
As easy as it may sound to a seasoned coder. I am a newbie trying to implement this on my clock page. It can contain errors. The idea is to generate a zero in front of single digits (like "02" instead of "2") for display purposes. It works fine with double digits.
This is what I got, but doesn't do the trick. Includes commented lines of different tries I have done. I would appreciate any input guys.
<script>
$(function() {
getdata();
myinterval = setInterval(getdata, 30000);
});
function getdata(){
var dt = new Date();
console.log(dt.getMinutes());
var myhr = dt.getHours();
var mymin = dt.getMinutes();
//if(myhr < 10) myhrstr = '0' + myhr.toString(); else myhrstr = myhr.toString();
//if(myhr.toString().length < 2) myhrstr = '0' + myhr.toString(); else myhrstr = myhr.toString();
//if(myhr.toString().length < 2) myhr = "0"+myhr;
if(myhr.toString().length == 1) myhrstr = "0" + myhr.toString(); else myhrstr = myhr.toString();
//if(mymin < 10) myminstr = '0' + mymin.toString(); else myminstr = mymin.toString();
//if(mymin.toString().length < 2) myminstr = '0' + mymin.toString(); else myminstr = mymin.toString();
//if(mymin.toString().length < 2) mymin = "0"+mymin;
if(mymin.toString().length == 1) myminstr = "0" + mymin.toString(); else myminstr = mymin.toString();
var mystr = myhrstr + myminstr;
$.ajax(
{
url:"clock.php?action=getdata&dt="+mystr,
success:function(result){
$('#content').html(result);
}
}
);
}
</script>
What makes you think your code isn't working? It works fine.
Here's a demo:
function FakeDate() {
this.getHours = function() {
return Math.round(Math.random() * 23);
}
this.getMinutes = function() {
return Math.round(Math.random() * 59);
}
}
var dt = new FakeDate(); // use fakeDate for random time generations
var myhr = dt.getHours();
var mymin = dt.getMinutes();
if (myhr.toString().length == 1) myhrstr = "0" + myhr.toString();
else myhrstr = myhr.toString();
if (mymin.toString().length == 1) myminstr = "0" + mymin.toString();
else myminstr = mymin.toString();
var mystr = myhrstr + myminstr;
console.log(mystr);
One way you can simplify this code is, since you are not using the numeric values, you can call toString right away on the getHours and getMinutes methods. For the same reason there's also no need for extra variables to hold the string values, you can just use the same variable when appending the "0".
// get the strings representing hours and minutes
var myhr = dt.getHours().toString();
var mymin = dt.getMinutes().toString();
// prepend them with zeros if needed
if (myhr.length == 1) myhr = "0" + myhr;
if (mymin.length == 1) mymin = "0" + mymin;
// concatenate them to a 4 digit value
var mystr = myhr + mymin;
Here's a demo:
function FakeDate() {
this.getHours = function() {
return Math.round(Math.random() * 23);
}
this.getMinutes = function() {
return Math.round(Math.random() * 59);
}
}
var dt = new FakeDate(); // use fakeDate for random time generations
// get the strings representing hours and minutes
var myhr = dt.getHours().toString();
var mymin = dt.getMinutes().toString();
// prepend them with zeros if needed
if (myhr.length == 1) myhr = "0" + myhr;
if (mymin.length == 1) mymin = "0" + mymin;
// concatenate them to a 4 digit value
var mystr = myhr + mymin;
console.log(mystr);
simple trick to pad single digit with zero
('0' + 1).slice(-2) // 01
('0' + 23).slice(-2) // 23
var mystr;
if(myhr < 10 ) {
mystr = "0" + myhr.toString();
} else {
mystr = myhr.toString();
}
if (mymin < 10) {
mystr += ":0" + mymin.toString();
} else {
mystr += ":" + mymin.toString();
}