Datetime.now.getTime() not returning correct value

  • I am getting a timestamp for current datetime using the following code

    public Static String getGenerateTimeStamp()
    {
        system.debug('Datetime : '+ DateTime.now());
        system.debug('timestamp : '+ DateTime.now().getTime());
        return String.valueof(DateTime.now().getTime());
    }
    

    21:21:46.752 (752692000)|USER_DEBUG|[51]|DEBUG|Datetime : 2014-03-20 10:21:47

    21:21:46.752 (752884000)|SYSTEM_METHOD_ENTRY|[52]|System.debug(ANY)

    21:21:46.752 (752891000)|USER_DEBUG|[52]|DEBUG|timestamp : 1395310907476

    I am passing this timestamps as an part of an API request and it gave out a error message

    The timestamp is expressed in the number of seconds since January 1%2C 1970 00%3A00%3A00 GMT. The timestamp value MUST be a positive integer.

    On checking with some online convertors the datetime is of a date in 1985. I am not sure if its the getTime() method which is at fault . The timestamp value of 1395310907 seems to give the correct date. I am not sure from where the 476 is getting appended.

  • Torsen

    Torsen Correct answer

    7 years ago

    The timestamp that salesforce returns are the number of millisecons from January 1 1970, not the number of seconds.

    Time for coffee i guess :P

License under CC-BY-SA with attribution


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

Tags used