1

I need to display a formatted date from a timestamp provided by Google Analytics Standard solution like

var date = new Date(timestamp * 1000);
var formatted = date.toString();

produces wrong value Jan 01 1970. That's because of timestamp format.

In PHP I can specify the timestamp format:

\DateTime::createFromFormat('Ymd', $timestamp);

How to do this in JS?

ymakux
  • 3,415
  • 1
  • 34
  • 43
  • Where is `timestamp` coming from? Are you sure it is in seconds, not milliseconds? If it is in seconds, that should work. What number is contained in `timestamp`? If it is `0`, or something that coerces to `0`, such as an empty string, then `0 * 1000` is `0` and `new Date(0)` returns a date object referring to the Unix Epoch, or Jan 01, 1970. – Useless Code Apr 07 '17 at 12:14
  • For example 20170306. It should be 06.03.17 – ymakux Apr 07 '17 at 12:19
  • @ymakux are you sure? `20170306` = 06.03.17? If so, I'm afraid that is not a unix timestamp, `20170306` is `08/22/1970 @ 10:51am (UTC)` Try yourself http://www.unixtimestamp.com/ – Adnan Umer Apr 07 '17 at 12:31
  • 1
    @ymakux That is not a Unix timestamp, it's just a date formatted in YYYYMMDD. A [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time) is the number of seconds since 1970-01-01. The correct Unix time stamp for 2017-03-06 would be `1488787200000`. If it is just a YYYYMMDD formatted date, that is easy enough to parse: `new Date(timestamp.slice(0, 4), timestamp.slice(5, 6) - 1, timestamp.slice(7, 8))`. (The `- 1` is because the Date constructor expects a 0-based month). – Useless Code Apr 07 '17 at 12:35
  • 1
    Using `- 1` with the string that `slice` produces implicitly coerces it to a number. You might want to explicitly coerce it using `parseInt` to make sure the intent of the code is clear: `new Date(timestamp.slice(0, 4), parseInt(timestamp.slice(5, 6), 10) - 1, timestamp.slice(7, 8))`. – Useless Code Apr 07 '17 at 12:41
  • 1
    D'oh. The correct Unix time stamp would be `1488787200`, not `1488787200000` I generated it with JS code, which generates the time in milliseconds, not seconds and forgot to divide by 1000 to get seconds. – Useless Code Apr 07 '17 at 12:45
  • @Useless Code Almost. First code - Nov 30 1899, second - Invalid date – ymakux Apr 07 '17 at 12:46
  • @@Useless Code Thanks, I just divided the "timestamp" using slice() as you proposed. You can post your your second comment as an answer an I'll accept it – ymakux Apr 07 '17 at 13:07
  • Already did :-) – Useless Code Apr 07 '17 at 13:16
  • http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – HarisH Sharma Apr 07 '17 at 13:19

3 Answers3

1

Since the dates you are receiving are formatted as YYYYMMDD, not as a Unix timestamp, you can parse it by extracting the year, month and date using String.prototype.slice.

var timestamp = '20170306',
  year = parseInt(timestamp.slice(0, 4), 10),
  month = parseInt(timestamp.slice(5, 6), 10),
  day = parseInt(timestamp.slice(7, 8), 10);
  // - 1 because the Date constructor expects a 0-based month
  date = new Date(Date.UTC(year, month - 1, day)),
  gmt = date.toGMTString(),
  local = date.toString();

console.log('GMT:', gmt); // Mon, 06 Mar 2017 00:00:00 GMT
console.log('Local:', local); 

This assumes that the dates you are using are in UTC (which they likely are). Date.UTC creates a timestamp (in milliseconds since Unix epoch) and then feeds it into new Date() which uses it to create a Date object representing that time. .toGMTString() outputs the date formatted for the GMT timezone. To output it formatted in local time, use .toString() instead.

Useless Code
  • 12,123
  • 5
  • 35
  • 40
0

try this type:

var userDate = new Date();
var day = userDate.getDate();
var month = userDate.getMonth() + 1;
var year = userDate.getFullYear();
alert("Date Formate is :"+year+"-"+month + "-" + day);
BalaMurugan.N
  • 120
  • 1
  • 1
  • 10
0

In javascript you can use an external library like moment.js

var date = moment.unix(timestamp);
date.format("YYYY MM DD");

See detail about .format here https://momentjs.com/docs/#/displaying/format/

Adnan Umer
  • 3,669
  • 2
  • 18
  • 38