function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC n12SFDC n12 

method to convert date time to date format in javascript

Hi,

I want to convert a datetime field to date field in javsscript

var date=record.LastModifiedDate;

what is the method i can use to convert it

Thanks
pconpcon
The record datetime field should be the seconds since the epoc.  So you should be able to do:

var date = new Date(record.LastModifiedDate);
console.log(date.toDateString());
SFDC n12SFDC n12
Hi,

now its displaying in this format


Fri May 09 2014 10:30:50 GMT+0530 (India Standard Time)

what i need is not the time only the date

from 

2013-01-28T17:09:21.000Z

to 

2013-01-28


pconpcon
Then you can do

date.toISOString().slice(0,10);

Unfortunately there doesn't seem to be any standard way to do this with the Date object directly in javascript
SFDC n12SFDC n12
it still displays int he below format only


Fri May 09 2014 10:30:50 GMT+0530 (India Standard Time)


var date = new Date(record.LastModifiedDate);
  date.toISOString().slice(0,10);
James LoghryJames Loghry
You can use Apex's Datetime.format(String str) method to represent a date string in whichever format you need.  (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm#apex_System_Datetime_format_2)

To get YYYY-MM-DD format from your last post you would do something similar to:

Datetime.now().format('YYYY-MM-dd');

For the format, you can follow the guidelines from the SimpleDateFormat class in Java here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Note: If you're field is of a "Date" type, instead of "Datetime" then you will need to convert it to a Datetime class.  
SFDC n12SFDC n12
can you help me with the code pls

var date = new Date(record.LastModifiedDate);
  date.toISOString().slice(0,10);

pconpcon
If you do:

var date = new Date(record.LastModifiedDate);
console.log(date.toISOString().slice(0,10));

You do not get "2014-05-09" in the console?
SFDC n12SFDC n12
no,

its displaying in the below format only

Fri May 09 2014 10:30:47 GMT+0530 (India Standard Time)
pconpcon
It looks like the toISOString is not being used correctly.  The following method should do what you want
function getFormattedDate(date) {
     var year = date.getFullYear();
     var month = (1 + date.getMonth()).toString();
     month = month.length > 1 ? month : '0' + month;
     var day = date.getDate().toString();
     day = day.length > 1 ? day : '0' + day;
     return year + '-' + month + -/' + day;
}
Date d = new Date(record.LastModifiedDate);
console.log(getFormattedDate(d));
SapereAude1337SapereAude1337
I had this issue when using a Lightning Component, after saving a record. The returned record had a datetime in a date field, so the rerendered component would get an error trying to put the datetime into the date input, resulting in the field emptying in the UI (was correct in actual data).

Date field value recieved from callback response: 2018-03-25T00:00:00.000Z
After casting that value to new Date(field) as suggested by pcon: Sat Mar 24 2018 17:00:00 GMT-0700 (Pacific Daylight Time)
--> this was making me lose a day due to time conversion

I solved this by using substring on the field value recieved from the response before passing it back to the component. 
object = response.getReturnValue(); // 2018-03-25T00:00:00.000Z
object.Due_Date__c = object.Due_Date__c.substring(0, 10); // 2018-03-25
component.set("v.attributeName", object);

 
Vignesh S 50Vignesh S 50
In LWC Instead converting from datettime to date why can't we convert our date to datetime using default JS functionalites like, (var newStartDate = new Date(this.newStartdatefield). So now we can do all our computations easily