Javascript - Setting Custom Time in Unix Timestamp - javascript

I'm working on a program that takes input from a user for a time. The program will take the information and automatically generate a Unix Timestamp using the current date as the date in the timestamp.
For example:
Daniel wants to generate a Unix Timestamp for 8:30 AM on Christmas
Day. He runs a command /unix 8:30, and the console prints out
1640421000.
What's the best way to achieve this? I understand how to generate a Unix Timestamp, but how do I edit just the time to the user input. Any help would be greatly appreciated.

You can create a Date for the current date, set the time as required, then generate a time value in seconds.
If the user will always enter H:mm and you don't need to validate the input, then the following will do:
let time = '8:30';
let tv = new Date().setHours(...(time.split(/\D/)), 0) / 1000 | 0;
// Check value
console.log(new Date(tv * 1000).toString());
However, the input should be validated (e.g. hours 0-23, minutes 0-59).

I just went with the following:
const time = interaction.options.getString('time');
const date = new Date().toISOString().slice(0, 10);
console.log(Math.round(new Date(`${date} ${time}:00`).getTime()/1000));
Seems to work for me.

Related

How to convert UK DateTime to GMT time in angular

I am working on a angular project.
I want to the start time and off time of a office in a table.
Example : 8:30 - 17:30
The office has two branches in UK and India.
So user may enter time in UK time or Indian time. But I am going to store the time in GMT time.
SO I want to give an option to select the time zone and enter the time.
Then I have to convert then that time into GMT time and send to database.
How can I convert UK time to GMT time?
Or do you know any idea to do this scenario Please advice me.
Thank you
ECMAScript Date instances don't have a timezone, they are inherently UTC. System settings are used for default toString plus get and set methods so they appear to be local.
Also, the parser is very basic and is generally to be avoided other than for parsing the exact format specified for toISOString.
You should also see How to initialize a JavaScript Date to a particular time zone, which might be a duplicate for this question.
The best way to achieve what you're after (until the Temporal object is widely supported) is to use a library. There are a number of libraries that work with timezones, the following uses Luxon.
// Alias
let DateTime = luxon.DateTime;
// Create a date for now in London
let ukDate = DateTime.now().setZone('Europe/London');
// Set it to the required time
let ukOpenTime = ukDate.set({hour:8, minute:30, second:0, millisecond: 0});
console.log('ukOpenTime as string :' + ukOpenTime.toString());
// Get time value to store
let millis = ukOpenTime.toMillis();
console.log('ukOpenTime as time value: ' + millis);
// Show equivalent local time in numerous ways
// Shift ukOpenTime to local zone: default is the local (system) timezone
let localOpenTime = ukOpenTime.setZone();
console.log('Equivalent local time 1 : ' + localOpenTime.toString());
// Use the time value (millis) to create a new Luxon object
let localOpenTime2 = DateTime.fromMillis(millis);
console.log('Equivalent local time 2 : ' + localOpenTime2.toString());
// Use the time value (millis) to create a plain date
let localOpenTime3 = new Date(millis);
console.log('Equivalent local time 3 : ' + localOpenTime3.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/3.0.1/luxon.min.js"></script>
why not store it in Epoch Time and convert it every time? like 1659755549?
or use epoch as intermediate for calculation
p.s. no rep for comments :(

Can you convert results from input type date and input type time to seconds since epoch

I am making a routing system and want to add a function so you can enter a start date and time through an HTML form, then JavaScript will add the route time to it and display it on the page.
However I'm having a bit of trouble with getting the date and time in the right format.
You can get the time in seconds with this:
var myDate = new Date();
myDate.getTime() / 1000;

Javascript Timestamp to User localzone

I want to convert my timestamps to the users local time.
I know that i get the localzone with that
var offset = new Date().getTimezoneOffset();
But i dont know how to calculate that with my timestamp to the localtime of the user...
I have the timestamp from that function
function time() {
return parseInt(new Date().getTime()/1000);
}
i get that 1544020722 timestamp and want not the hours and minutes of it but i want it in the local time of the user in javascript
offset give me only -60 but i dont know how to calculate that with my timestamp i have to show the user the correct time :/
I also doesnt find a function where i can easy "send" my timestamp and it gives back the current time for the localtime user
The best way is, if someone has a function where ich can easy convert that like
toUsersLocaltime(1544020722)
or something.
Thanks for your help

why moment js cant handle the convert :|?

I am using the Persian date picker and get the Unix time with it
and just want to convert to Gregorian.
here's my code:
persian date picker Unix time => 1532967741167
let unix = 1532967741167
let date = moment.unix(unix).format("YYYY-MM-DD");
console.log(date);
heres what i get from moment => 50547-10-25
here's what I get from epochconverter.com
and it's correct
any idea what's going on in here ?! :|
The problem is that you have to divide by 1000
let unix = 1532967741167/1000
this happens beacause as momentjs docs implies
To create a moment from a Unix timestamp (seconds since the Unix
Epoch), use moment.unix(Number). This is implemented as
moment(timestamp * 1000), so partial seconds in the input timestamp
are included.
here is the link

Meteor - MongoDb - How to get seconds remaining between current data time and future date time

I want to get seconds remaining between a current dataTime and future dateTime.
I am using Meteor + MongoDb.
In Mongo DataTime is saved like this:
2015-12-11T06:14:39.671Z
I want seconds remaining between current datatime and future or expiry datetime.
keep your time data in epoch format which is basically storing data in milliseconds. then you'll be easy able to compare using momentJS or substracting. And in MongoDB , this data will be stored as Number type not Date
var time= (new Date).getTime();
this piece of code will return time in milliseconds.
The javascript Date can handle the sample input as listed in your question. If that format is consistant, the following should get you your answer:
var difference = (new Date(databaseDate).getTime()-Date.now())/1000;
Where Date.getTime() and Date.now() are in milliseconds (hence the 1000).

Categories