0
var stardate = "02-March-2016";

How to convert the stardate string into UNIX timestamp. Is there is any inbuilt function in javascript for this?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
AspDev18
  • 87
  • 2
  • 12
  • 4
    Possible duplicate of [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Umur Dinçer Sep 27 '19 at 10:58
  • You can do `new Date("02-March-2016").getTime()` – Nick Parsons Sep 27 '19 at 10:58
  • 1
    Possible duplicate of [Javascript Convert Date Time string to Epoch](https://stackoverflow.com/questions/13707333/javascript-convert-date-time-string-to-epoch) – Nick Parsons Sep 27 '19 at 10:59

1 Answers1

1

To get the unix-timestamp do this:

new Date("02-March-2016").getTime() / 1000;
Jonathan
  • 1,955
  • 5
  • 30
  • 50
  • Thanks for this answer, new Date("02-March-2016").getTime() / 1000; . This thing is worked, but I have a question when I convert date to Unix Timestamp using this above code the result is slightly different from PHP inbuilt function "strtotime" time, why? – AspDev18 Sep 28 '19 at 17:43
  • How does it differ? – Jonathan Sep 28 '19 at 19:13
  • I mean to say that is why JavaScript and PHP is giving different results, while both have to do the same kind of work. – AspDev18 Sep 28 '19 at 19:41
  • What are these differences in the results? Can you give an example? – Jonathan Sep 29 '19 at 01:29