8

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.

3 Answers 3

16

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

1
  • 3
    Time for coffee i guess :P
    – Prady
    Mar 20, 2014 at 11:12
5

The documentation for getTime() states:

getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.

You're passing milliseconds into something that is expecting seconds. You could divide 1000 to convert it to seconds.

2

getTIme() method returns the time in milliseconds.Therefore last 3 digits corresponds to millisecond time. You can get the millisecond by using DateTime.now().millisecond() and then subtract it from DateTime.now().getTime()

1
  • Or just divide by 1000 Mar 20, 2014 at 19:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .