0

I would like to change the date format in my code like that - > 19.7.2016

Could anyone help me, please?

function calcWorkingDays(fromDate, days) {
    var count = 0;
    var m = new Date();
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
            count++;
    }
    return fromDate;
}
alert(calcWorkingDays(new Date(), 4));
npinti
  • 51,780
  • 5
  • 72
  • 96
Daniel
  • 13
  • 3

2 Answers2

0

You need to format the dateobject to your likeness:

Extract the Day, Month and Year from your DateObject:

var month = fromDate.getUTCMonth() + 1; //months from 1-12
//January=0, February=1, etc

var day = fromDate.getUTCDate();
var year = fromDate.getUTCFullYear();

newdate = day + "." + month + "." + year;

Complete Example:

function calcWorkingDays(fromDate, days) {
    var count = 0;
    var m = new Date();
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
            count++;
    }

    var month = fromDate.getUTCMonth() + 1;
    var day = fromDate.getUTCDate();
    var year = fromDate.getUTCFullYear();

    newdate = day + "." + month + "." + year;

    return newdate;
}
alert(calcWorkingDays(new Date(), 4));

Other possible examples are:

new Date().toISOString()
"2016-02-18T23:59:48.039Z"
new Date().toISOString().split('T')[0];
"2016-02-18"
new Date().toISOString().replace('-', '/').split('T')[0].replace('-', '/');
"2016/02/18"

new Date().toLocaleString().split(',')[0]
"2/18/2016"

source

Community
  • 1
  • 1
Igoranze
  • 1,506
  • 13
  • 35
0

You should convert date to string:

function calcWorkingDays(fromDate, days) {
    var count = 0;
    var m = new Date();
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
        count++;
}
     return fromDate.getDate()  + "." + (fromDate.getMonth()+1) + "." + fromDate.getFullYear();

}

alert(calcWorkingDays(new Date(), 4));