- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 07:06 AM
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);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 07:15 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 07:15 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader