Setting values of a date field

MaharshiC
Tera Contributor

Hi,

 

I have a date field and it uses the format dd-mm-yy. So I have written the client script to manipulate the date from the script and fill the current date there in the field. However the below script is not returning any value. Can anyone please check ? It is returning correct value if i show the entire year instead of the last 2 digits of the year but i need last 2 digits as that fits the date format.

var month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    var date = new Date().getDate();
     var monthNumber =new Date().getMonth();
     var name = month[monthNumber];
	if (date<10)
	{	
	var newDate='0'+new Date().getDate()+'-'+name +'-'+ (new Date().getFullYear()).slice(-2);
	alert(newDate);
		}
	else
	{
		newDate=new Date().getDate()+'-'+name +'-'+(new Date().getFullYear()).slice(-2);
               alert(newDate);
	}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@MaharshiC 

this will work.

it fails when you slice the number. use toString()

var month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var date = new Date().getDate();
var monthNumber =new Date().getMonth();
var name = month[monthNumber];
if (date<10)
{
var newDate='0'+new Date().getDate()+'-'+name +'-'+ (new Date().getFullYear()).toString().slice(-2);
alert(newDate);
}
else
{
newDate=new Date().getDate()+'-'+name +'-'+(new Date().getFullYear()).toString().slice(-2);
alert(newDate);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@MaharshiC 

this will work.

it fails when you slice the number. use toString()

var month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var date = new Date().getDate();
var monthNumber =new Date().getMonth();
var name = month[monthNumber];
if (date<10)
{
var newDate='0'+new Date().getDate()+'-'+name +'-'+ (new Date().getFullYear()).toString().slice(-2);
alert(newDate);
}
else
{
newDate=new Date().getDate()+'-'+name +'-'+(new Date().getFullYear()).toString().slice(-2);
alert(newDate);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader