i want to access only date form datetime

  • If i used System.debug('date='+DATEVALUE(System.now())); in developer console i am getting error msg by saying:

    method does not exist or incorrect signature for DATEVALUE(datetime)

    How i can do this.

    i have custome object in that object senderdate field is available with datetime type. suppose this filed data is

    2013-08-02 00:20:00
    2013-08-02 01:40:00
    2013-08-02 00:20:00
    2013-08-04 00:20:00
    

    i want to access all row with same date i don't want time consideration.

    i.e senderdate= 2013-08-02
    it should display me 3 rows..
    

    DATEVALUE is a Formula function, it can not be used in Apex, only in Visualforce, Custom Formula Fields, Validation Rules etc. You need to use the approach described in mast0r answer.

  • You can use a date() method of the Datetime object:

    System.debug('date=' + (System.now()).date());
    

    Alternatively you can use valueOf() method of Date object:

    System.debug('date=' + Date.valueOf(System.now()));
    

    Is SOQL you can use a Date variables to compare a CreatedDate for example:

    Date d = System.today();
    
    List<account> a = [Select Id From Account Where CreatedDate = :d];
    

    Datetime Methods

    Date Methods

    if i use System.now()).date() it is giving me date plus 00:00:00 for time.

    @JayantJadhav Yes, this is a new Date object.

    ya can i use date() function in soql. ex:

    [select name from myobject where datetimefile__c.date()=System.now().date()]

    i will edit my question just have a look what is my requirement.

    If you want to get a date string from a datetime just use e.g. system.now().format('yyyy-MM-dd'). This way you can control what the output looks like. Just mentioning this as your question involved debugging a string value of a datetime variable...

    Actually, if you use `Date.valueOf`, it does not strip out time information, strangely enough. Try `system.debug(Date.valueOf(Datetime.now()))` and see for yourself.

  • Simply use

    System.debug('date=' + (System.now()).date().format());

    this will result 2013-08-02 00:20:00 to 02-08-2013

  • You can Use this

    <apex:outputText value="{0,date,yyyy.MM.dd}">
       <apex:param value="{!MyVar.Holiday_Date__c}"/>
    <apex:outputText>
    

    Where ..... instead of MyVar.Holiday_Date__C use your DateTime variable name.

  • System.debug('date='System.today()); will return current date where as 
    
    
    System.debug('date='+System.now());   will retugn current date and time
    
  • You can use the Date.format method which:

    "Converts the date to the local time zone and returns the converted date as a string using the supplied Java simple date format. If the time zone cannot be determined, GMT is used."

    If you use System.now()).date().format() it will return something like 4/29/2021

  • Use String Class Split to trim time
    System.debug(String.valueof(System.now()).split('\\s[0-9,:]*')[0]);
    returns USER_DEBUG|[1]|DEBUG|2017-10-25

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used