0

I was given data.json file with the dates in the following format:

"creationTime": 1542111235544,  
"creationTime": 1545880457898,  

Which translated to:

13/11/2018, 13:13:55
27/12/2018, 04:14:17

by doing:

new Date(ticket.creationTime).toLocaleString()

How can I do the backward operation? For example, if I have a date as:

27/09/2014

How can I get the code like in the others? The time is optional (I can add 00:00 am if needed)

Thanks a lot

zhulien
  • 5,145
  • 3
  • 22
  • 36
Iam Spano
  • 31
  • 6
  • Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – VLAZ Mar 06 '21 at 18:00
  • No. I don't even see my format mentioned – Iam Spano Mar 06 '21 at 18:04
  • 1. Few answers show that one 2. even the ones that don't show how to handle the data. You need to separate it into parts and give it to the `Date` constructor. – VLAZ Mar 06 '21 at 18:06
  • I need to do the backward operation. They're all talking about how to convert dates etc. I need the json code (don't even know the name of it), that I mentioned above – Iam Spano Mar 06 '21 at 18:07
  • That is not what I need. I need the number, the code, such as: 1542111235544. Example: convert from 13/11/2018, 13:13:55 to 1542111235544 – Iam Spano Mar 06 '21 at 18:10
  • [How do you get a timestamp in JavaScript?](https://stackoverflow.com/q/221294) – VLAZ Mar 06 '21 at 18:11
  • I appreciate the help, but the question asks about how to get a current timestamp, the current time. Doesn't deal with conversions. – Iam Spano Mar 06 '21 at 18:14
  • You can convert the from the format yourself using the first link, then get the timestamp using the second link. There is no built-in functionality for that. – VLAZ Mar 06 '21 at 18:15
  • Have you tried https://momentjs.com/docs/#/displaying/ library for your needs. I don't know if the above format matches but you can explore – vibhor vaish Mar 06 '21 at 18:17
  • It sounds like you're trying to convert a string back to milliseconds, which would be something like this: `new Date('12/27/2018, 04:14:17').getTime()`? – user1791914 Mar 06 '21 at 18:18
  • 1
    @user1791914 this may not produce the correct results since it's not a standard date format. – VLAZ Mar 06 '21 at 18:19

1 Answers1

0

You can try using this code: Noted date format used here mm/dd/yyyy

  var dateToNumber=new Date("09/27/2014").getTime(); //input month/date/year
console.log("dateToNumber",dateToNumber);

  var numberToDate=new Date(1411754400000).toLocaleString();
console.log("numberToDate",numberToDate);
  • This is not culture agnostic. Bad general solution. – zhulien Mar 06 '21 at 18:28
  • 1
    `"09/27/2014"` is not a standard date format, so the interpretation of it would be implementation dependant. – VLAZ Mar 06 '21 at 18:28
  • @zhulien, the comments he already mentioned about the back process he wants to generate number format from the date-time. I am not thinking about the date format. Just implement the flow of the process. – Palash Kanti Bachar Mar 06 '21 at 18:43
  • @VLAZ just implement the process of back to generate the number from the date. – Palash Kanti Bachar Mar 06 '21 at 18:44
  • @PalashKantiBachar Like I said, it is a pretty bad solution and you actually demonstrated exactly that. As you see, his date is `27/09/2014` and yours - `new Date("09/27/2014")`. Yes, it is the same date but in different format. Culture format should be taken into consideration and ignoring it like that is a pretty bad sign. – zhulien Mar 06 '21 at 18:48