0

Say, i have below information:

Asia zone, current date say 7/23/2018 and time say 2.25 pm.

With these three values, i want to generate timestamp number like 90001333... is that possible? in javascript or C#

swifty
  • 165
  • 1
  • 1
  • 17

2 Answers2

0

Use following:

var time = '2.25 pm'
var date = new Date('7/23/2018');
var isPm = time.indexOf('pm') != -1;
time = time.replace(/pm/g, '');
time = time.replace(/am/g, '');
var spl = time.split(".");
date.setHours(isPm ? 12 + Number(spl[0]) : spl[0], spl[1], 0);
var st = date.toLocaleString('en-US', {
  timeZone: 'Asia/Kolkata'
});


var timestamp = Number(new Date(st));
console.log(new Date(st))
console.log(timestamp)

You can use the above logic to complete the quest.

Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28
  • @RobG can you verify this solution. – Ullas Hunka Jul 23 '18 at 09:19
  • No. It uses the built-in parser to parse a non–standard string which, **if** parsed correctly, will be treated as local. So a different UTC date and time depending on the host offset. It then outputs a result based on that UTC value and the timezone rules for Asia/Kolkata which may or may not have the same offset and daylight saving rules as other parts of Asia (they vary from +2 to +10). It's not difficult to do what the OP wants, there are answers here already, but until the timezone of the input string and expected output are known, this question can't be answered. – RobG Jul 23 '18 at 22:14
-1

If you want plain and unreliable in some cases JS:

var unixTime = Date.parse("7/23/2018 02:25:00 GMT+8")

or with Moment.js

var date = moment("2016-10-11 18:06:03").tz("Asia/Bangkok").format();
var unixTime = moment(date).format("X");
bMain
  • 324
  • 3
  • 11
  • [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) How is the timezone accommodated? 2:25 pm is 14:25. – RobG Jul 23 '18 at 09:08
  • Added an alternative – bMain Jul 23 '18 at 09:14