0

My server back end sends the time value as milliseconds (1479515722195). I use a conversion via a library function and get it in date as Sat Nov 19 2016 11:35:22. How can I separate date and time? I only need its date to be used for further processing.

Current value in ms : 1479515722195
Current value after conversion : Sat Nov 19 2016 11:35:22
Expected value removing time : Sat Nov 19 2016
esote
  • 831
  • 12
  • 25
user3916007
  • 189
  • 1
  • 9
  • `theValue.substring(0, 15)` (it should be a fixed format) –  Nov 19 '16 at 00:41
  • To reliably format dates, use a small library like [*fecha.js*](https://github.com/taylorhakes/fecha) or a big one like [*moment.js*](http://momentjs.com) if you have other things you want to do. – RobG Nov 19 '16 at 02:25

2 Answers2

0

The easiest way is to stringify Date object and cut off the part you don't want. If you only want date, you can write

(new Date(1479515722195) + '').substring(0, 15);

It will produce string 'Sat Nov 19 2016'.

  • The format of *Date.prototype.toString* is implementation dependent, so this will not necessarily return what the OP wants. – RobG Nov 19 '16 at 02:21
  • @RobG. Considering v8 and Gecko as most popular ES engines, I really couldn't find an other implementation which doesn't have this format. –  Nov 19 '16 at 11:43
  • The point is not whether you can find where it fails, but that there is no specification requiring any particular format. So your solution depends on convention. ;-) – RobG Nov 20 '16 at 23:45
0

just use the native Date object and get the pieces you need independently

var d = new Date(1479515722195);
console.log((d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear())

outputs

11/18/2016
thedarklord47
  • 3,183
  • 3
  • 26
  • 55