How to insert space between two values while using protractor? - javascript

I am creating a script to enter date in some specific field, the format it requires is MMM DD, YYYY, as you can see format has 2 spaces one between month name and date and other between comma and year. I searched so many places and tried below code but it returns value as NaN, 2018 my code is listed below-
this.getCurrentDate = function () {
var d = new Date();
var currentDate = d.getDate();
var currentMonth = d.getMonth()+1;
var currentYear = d.getFullYear();
if (currentDate < 10){
currentDate = '0'+currentDate;
}
if (currentMonth < 10){
currentMonth = '0'+currentMonth;
}
var today = currentMonth + '\xa0' + currentDate-1 + ',' + '\xa0' + currentYear;
console.log(today);
return today;
};

Your issue lies with this line of code:
var today = currentMonth + '\xa0' + currentDate-1 + ',' + '\xa0' + currentYear;
So previously you set these two variables to strings:
if (currentDate < 10){
currentDate = '0'+currentDate;
}
if (currentMonth < 10){
currentMonth = '0'+currentMonth;
}
In the variable today at currentDate you're trying to subtract the integer 1 from a string. Hence, NaN (Not a Number).

You are subtracting 1 from a string which is causing error.
this.getCurrentDate = function () {
var d = new Date();
var currentDate = d.getDate();
var currentMonth = d.getMonth()+1;
var currentYear = d.getFullYear();
if (currentDate < 10){
currentDate = '0'+currentDate;
}
if (currentMonth < 10){
currentDate--; //Fixed here.
currentMonth = '0'+currentMonth;
} //removed -1 from current date
var today = currentMonth + '\xa0' + currentDate + ',' + '\xa0' + currentYear;
console.log(today);
return today;
};
You have to subtract value from a number before you change it to a string. This will work now.

Related

JavaScript or Jquery - How to display last week of the month and next month's first Monday

How do you display only when it's the last week of the month (the text below).
The DATE that will be displayed here is next month's first Monday.
"Due on DD/MM/YY"
To detect if this is the last week in month you can follow this example:
https://www.javatpoint.com/calculate-current-week-number-in-javascript
To get next monday, you can call this function:
getNextMonday(){
var date = new Date();
date.setDate(date.getDate() + (1 + 7 - date.getDay()) % 7);
return date;
}
You can use the below JS Code. It will show the alert on last week of month means from last monday of month.
function Get_Last_Monday_of_Month()
{
var year = new Date().getFullYear();
var month = new Date().getMonth() + 1;
var dat = new Date(year+'/'+month+'/1');
var currentmonth = month;
var firstmonday = false;
while (currentmonth === month)
{
firstmonday = dat.getDay() === 1 || firstmonday;
dat.setDate(dat.getDate()+(firstmonday ? 7 : 1));
currentmonth = dat.getMonth()+1;
}
dat.setDate(dat.getDate()-7);
return dat;
}
function Get_Next_Coming_Monday()
{
var date = new Date();
date.setDate(date.getDate() + (1 + 7 - date.getDay()) % 7);
return date;
}
var d1 = new Date();
var d2 = Get_Last_Monday_of_Month();
var d3 = Get_Next_Coming_Monday();
if (d1.getTime() >= d2.getTime())
{
var datestring = ("0" + d3.getDate()).slice(-2) + "/" + ("0"+(d3.getMonth()+1)).slice(-2) + "/" + d3.getFullYear();
document.write ("Due on " + datestring);
}

how to Add 1 year to startdate to get the end date in javascript

want a output like below
if Start Date is "01-03-2016" the End date should be "28-02-2017"
if Start Date is "10-04-2016" the End date should be "09-04-2017"
I tried below code
if (dat <= 31 && dat >= 1 && month <= 12 && month >= 1) {
var expiryDate = new Date(n1, month - 1, dat);
expiryDate.setFullYear(expiryDate.getFullYear() + 1);
var day = ('0' + expiryDate.getDate()).slice(-2);
var month1 = ('0' + (expiryDate.getMonth() + 1)).slice(-2);
var year = expiryDate.getFullYear();
var month = getMonthName(month1);
var wholeenddate = day + "-" + month + "-" + year;
but it's not produce desired output.Please Help to solve it.
Add 364 days to your date
For example
var d = new Date("2016-03-01");
d.setDate(d.getDate()+364); //outputs 28-02-2017
and
var d = new Date("2016-04-10");
d.setDate(d.getDate()+364); //outputs 09-04-2017
or Just add 1 year and sub 1 day.
d.setFullYear(d.getFullYear() + 1);
d.setDate(d.getDate()-1);
Now it will match your output just the same even for leap year :)
Demo
var d = new Date("2016-03-01");
d.setFullYear(d.getFullYear() + 1);
d.setDate(d.getDate()-1);
document.body.innerHTML += d.toString();
document.body.innerHTML += "<br>";
d = new Date("2016-04-10");
d.setFullYear(d.getFullYear() + 1);
d.setDate(d.getDate()-1);
document.body.innerHTML += d.toString();
There's a convenient library to help with this sort of thing - moment.js (14k zipped).
var startDate = moment('01-03-2016', 'DD-MM-YYYY');
console.log(startDate.format('DD-MM-YYYY'));
var endDate = startDate.clone();
endDate.add(1, 'years').subtract('1', 'days');
console.log(endDate.format('DD-MM-YYYY'));
3 ways to do this.
// Add hours
var today = new Date();
today.setHours(today.getHours()+24*364);
// Add days
var nextyearDate = new Date();
nextyearDate.setDate(today.getDate()+364);
// More reliable way : Add year & subtract a day.. Hope this works...! Works for 01/01/2016
var nextDate = new Date();
nextDate.setYear(nextDate.getFullYear()+1);
nextDate.setDate(nextDate.getDate()-1);

Converting date format from mm/dd/yyyy to yyyy-mm-dd format after entered

In my datepicker the date will be inserted in mm/dd/yyyy format. But after I inserted I want it to be sent in yyyy-mm-dd format. I am using JavaScript to do this. But I wasn't able to do that. So what should I do?
Thanks & regards,
Chiranthaka
you could also use regular expressions:
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
The expression also works for single digit months and days.
I did the opposite for my website, but it might help you. I let you modify it in order to fit your requierements. Have fun !
getDate
getMonth
getFullYear
Have fun on W3Schools
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
if(curr_month < 10)
curr_month = "0"+curr_month;
if(curr_date < 10)
curr_date = "0"+curr_date;
var curr_date_format = curr_date+"/"+curr_month+"/"+curr_year;
Adding more to Christof R's solution (thanks! used it!) to allow for MM-DD-YYYY (- in addition to /) and even MM DD YYYY. Slight change in the regex.
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})[\/ -](\d{1,2})[\/ -](\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
As Christof R says: This also works for single digit day and month as well.
// format from M/D/YYYY to YYYYMMDD
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear();
var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
return "".concat(yyyy).concat(mm).concat(dd);
};
var siku = new Date();
document.getElementById("day").innerHTML = siku.yyyymmdd();

javascript date format conversion [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I am using pikaday's datepicker. It gives me output "Fri Sep 20 2013". How can I convert this date into yyyy-mm-dd format and I also would want following date of this selected date and set that one to another element.
I tried this code
function formattedDate() {
var fromdate = new Date(document.getElementById('datepicker').value);
var dd = fromdate.getDate();
var mm = fromdate.getMonth()+1; //January is 0!
var yyyy = fromdate.getFullYear();
if(dd < 10)
{
dd = '0'+ dd;
}
if(mm < 10)
{
mm = '0' + mm;
}
var fromdate1 = dd+'/'+mm+'/'+yyyy;
fromdate.setDate(fromdate.getDate() + 2);
document.getElementById('datepicker').value = fromdate1;
var newdate = fromdate;
document.getElementById('datepicker1').value = newdate;
//alert(newdate1);
}
But it doesn't work.
var date = new Date(dateString);
var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var properlyFormatted = "" + year + month + day;
Or
var date = new Date(dateString);
var properlyFormatted = date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2);
Use momentjs — it's a library available for using in browser and node projects
In your case you should use this pattern:
moment().format("YYYY-mm-D");
And you can try it in console on momentjs's site:

Show current date

I am using the following to get the current date:-
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;
If I alert(newdate); it shows:-
3/06/2013
Is there any way I can display this as:-
03/06/2013
With plain Javascript, only manually
if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;
If you want to avoid using a library, and don't mind an extra line in your JavaScript:
var day = dateObj.getUTCDate(),
dd = parseInt(day, 10) < 10 ? '0' + day : day;
Using JQuery DateFormat:
$.format.date(dateObj.toString(), "dd/MM/yyyy");
var mydate=new Date();
alert(mydate.toString('dd/MM/yyyy'));
function padWithZeroes(number, width)
{
while (number.length < width)
number = '0' + number;
return number;
}
Now call day = padWithZeroes(day, 2) (and likewise for the month) before you use it.
You can split it and create your own format:
var splitTime = newDate.split("/");
var day = splitTime[0];
var month = splitTime[1];
var year = splitTime[2];
if (day < 10){
day = "0" + day;
}
var myDate = day + '/' + month + '/' + year;
Living demo:
http://jsfiddle.net/rtqpp/
Please use the following:
var dateObj = new Date();
var month = ('0' + (dateObj.getUTCMonth() + 1) ).slice( -2 );;
var day = ('0' + (dateObj.getUTCDate() + 1) ).slice( -2 );
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;

Categories

Resources