Don't do this
There are a number of issues with what OP is attempting, firstly with new Date("sep 01, 2020 01:59:59")
:
- The format is not supported by ECMA-262, so parsing is implementation dependent and it may or may not be parsed as expected
- Even if parsed correctly, the host system offset will be assumed so it will represent a different moment in time for each system with a different offset
Also see Why does Date.parse give incorrect results?
Problems with d.toLocaleString('de-DE', {timeZone: 'CET'})
:
- The exact output format is not specified, so implementations are free to convert the language "de-DE" to whatever format they think matches
- Whatever format is produced is not required to be parsable by the implementation that generated it, much less other implementations. E.g. given
new Date('31.8.2020, 17:59:59')
Safari and Firefox return an invalid Date
- The timezone is likely not included in the string, so the same issue arrises as with #2 above.
Do this instead
A reasonable approach would be to use some other parser for the string and associate the required timezone (a library can help greatly with that, either by adding it to the string or specifying it as an option). That should generate a suitable Date and also provide a method to get the time value, which is milliseconds so can be converted to seconds by dividing by 1000.
The only way to do this without a library is to mess with Intl.DateTimeFormat to work out what the offset should be, then manually apply it (per this anwer). Much simpler to use a library like Luxon:
let DateTime = luxon.DateTime;
// Timestamp to parse
let s = 'sep 01, 2020 01:59:59';
// Format of input timestamp
let fIn = 'MMM dd, yyyy HH:mm:ss';
// Location to use to determine offset when parsing
let loc = 'Europe/Paris';
let d = DateTime.fromFormat(s, fIn, {zone: loc});
// Show date using Luxon default format
console.log(d); // "2020-09-01T01:59:59.000+02:00"
// Show date in set format
let fOut = 'dd.MM.yyyy, HH:mm:ss ZZ';
console.log(d.toFormat(fOut)); // 01.09.2020, 01:59:59 +02:00
// UNIX timestamp in seconds
console.log(d.toFormat('X')); // 1598918399
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.24.1/luxon.min.js"></script>
You can do the same thing with other libraries, Luxon is just a convenient example. Note that with Luxon, once a location is associated with the object, it keeps using that location for offset and timezone data, which might be handy or annoying, depending on what you're trying to do.