0

Using timestamp I am able to Convert timestamp to actual-time like below. Ie 10:00 AM, and then sending this 10:00 AM to backend now while retrieving this time I need to convert this into timestamp as the component I am using is accepting timestamp only. So how to convert this time 10:00 AM to timestamp? I can put any static date and convert it into timestamp but how to pass this time to timestamp So my actual output 10:00 AM is preserved?

var time = new Date();
console.log(
 time.toLocaleString('en-US', { 
     hour: 'numeric', 
     minute: 'numeric', 
     hour12: true 
  })
);
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • 1
    `time.valueOf()` – Randy Casburn Oct 29 '20 at 04:59
  • time.valueOf() ? – Aman Sadhwani Oct 29 '20 at 05:03
  • 1
    `new Date().getTime() ` – Sarun UK Oct 29 '20 at 05:05
  • Does this answer your question? [Javascript Converting human time to timestamp](https://stackoverflow.com/questions/11172568/javascript-converting-human-time-to-timestamp) – somethingsmart Oct 29 '20 at 05:06
  • var time=new Date().getTime("10:00 AM") this way? – Aman Sadhwani Oct 29 '20 at 05:10
  • @AmanSadhwani - `time` is your variable name which contains a date object. `.valueOf()` provides the timestamp from any date object - thus: `time.valueOf()` – Randy Casburn Oct 29 '20 at 05:13
  • @somethingsmart var d = new Date("Wed Jun 20 10:47:00 +0000 2012"); This output for this is "Wed Jun 20 2012 16:17:00 GMT+0530 (India Standard Time) {}" – Aman Sadhwani Oct 29 '20 at 05:20
  • @RandyCasburn I dont have date object I just have time ie 10:00 AM – Aman Sadhwani Oct 29 '20 at 05:22
  • In your code snippet you provided this: `var time = new Date();` - and you referenced that code when you stated this: "_I am able to Convert timestamp to actual-time like below_". Excuse me for reading your question and your code. I won't bother you any longer. – Randy Casburn Oct 29 '20 at 05:30
  • "10:00 AM" is a timestamp. If by "timestamp" you mean milliseconds since the ECMAScript epoch, then there will be as many instances of "10 am" as there are days in the ECMAScript date range (approximately ±285,426 years from 1970) multiplied by the number of unique timezone offsets. Which one do you want to use? – RobG Oct 30 '20 at 05:36

1 Answers1

0

Remember, the Time you get in javascript is based on the browser/computer which it is running. Server may not be on the same Time Zone so if you try to pass a time which is from browwer to server, and if they are in different timezone, you can run into many issues.

Second, you should avoid sending "formatted" time from JS to server and do reverse processing on server.

Using UTC (universal coordinated time) solves the adjustment problems.

Coming to your question on how to get the time value on backend?, the answer will depend on what is your backend - java/.net etc Regardless of technology, you will have to create a DateTime variable based on the value you received from browswer/front-end and also take care of TimeZone adjustment.

Amit Jaiswal
  • 118
  • 8