How to convert date format

sheths
Kilo Explorer

Hello all,

I have an inbound action which reads content of the email and populates a table with the information from the email body.

My issues is that the email contains long format date e.g. 24th March 2014 at 23:59hrs and I need to convert this to short format dd/mm/yyyy so it can populate date field on the form.

Appreciate any help with this.

Thank you.

20 REPLIES 20

You mean you don't want to change the system property? Sorry, may be I didn't get. Can you please little bit explain.


Let me know if I can check your SNOW instance?


Hey Shishir, what I meant was when I try to set the 'Date' field value via a Script include, the format doesn't get applied. This is what I'm doing:



  1. setQuarterlyValidDate: function() {  
  2.   var gr = new GlideDateTime();  
  3.   gr.addMonthsUTC(3).setDisplayValue("dd/MM/yyyy HH:mm:ss");  
  4.   gr.getDisplayValue();  
  5.   return this._convertToGlideDate(gr);  
  6.   },  
  7.  
  8.   _convertToGlideDate: function (gdt){  
  9.   var gd = new GlideDate();  
  10.   gd.setValue(gdt.getValue());  
  11.   gd.getByFormat("dd/MM/yyyy");  
  12.   return gd;  
  13.   },

And this is the Client script...


  1. var answer = response.responseXML.documentElement.getAttribute("answer");
  2. var dFormat = g_user_date_format;
  3. var tdayDate = formatDate(new Date(answer),dFormat);
  4. g_form.setValue('u_review_date',answer
  5. );


Still, the Date field shows the value in 'yyyy-mm-dd' format & not in the 'dd/MM/yyyy format....   is there anything wrong???


Hi Suhas,



I see


In Script Include you are using gd.getByFormat("dd/MM/yyyy"); but that is not the function to format the date, that is being used to format the time, please check here: Scoped GlideTime API Reference - ServiceNow Wiki .


Also, gd.setValue(gdt.getValue()); this is also not correct, since you are not getting any value with getValue.



In Client script below doesn't make any sense since they are not being used and also don't know what it's purpose.


  1. var dFormat = g_user_date_format;  
  2. var tdayDate = formatDate(new Date(answer),dFormat);


Please try below below code if it helps.



Client Script:


function onLoad() {


  var dFormat = g_user_date_format;


  var tdayDate = formatDate(new Date(),dFormat);


  g_form.setValue('u_newdate', tdayDate);



  var ajax = new GlideAjax('MyNeededDateBy');


  ajax.addParam('sysparm_name', 'neededByDateTime');


  ajax.getXML(function () {


  var neededDate = ajax.getAnswer();


  g_form.setValue('u_mydate', neededDate);


  });


}



Script Include:


var MyNeededDateBy = Class.create();


MyNeededDateBy.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  neededByDateTime: function () {


  var neededDate = gs.nowDateTime();


  var gdt = new GlideDateTime(neededDate);


  gdt.setDisplayValue(neededDate, "dd/MM/yyyy hh:mm:ss");


  gdt.addMonthsUTC(3);


  return gdt.getDisplayValue();


  },


  type: 'MyNeededDateBy'


});



Note: In my instance System date format is yyyy-MM-dd and in User profile the Date Format is dd/MM/yyyy.


I am getting below result:


find_real_file.png


Thanks, its now working...used setDisplayValue("dd/MM/yyyy HH:mm:ss") and then getDisplayValue() methods to set the values in the Script Include and then in turn the client script just sets the value retrieved.



Cheers


Perfect Suhas, Happy to contribute something for fixing your issue.



Please mark answer helpful and/or correct if I helped in solving your issue.