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
Related
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.
I am trying to show time based on user local time zone. Server saves time to utc.
So, I have a date-time saved in my database.
2018-10-03 05:55:51 // my server is digital ocean
So now I am trying to console user local time. My time zone is set to
Sylhet Bangladesh
Time offset is -360
var offset = new Date().getTimezoneOffset();
console.log(offset) // offset is -360
var testDateUtc = moment.utc("2018-10-03 05:55:51");
var localDate = moment(testDateUtc).utcOffset(offset);
console.log(localDate.format("YYYY-DD-MM hh:mm:ss"));
The above code prints incorrect date but correct time.
2018-02-10 11:55:51 the date is wrong.
I then changed my mac's time zone to Dubai which is 2 hours different then my country
For dubai the offset is -240 and it shows time
2018-03-10 01:55:51 this mean the date is correct but time is not correct.
Please help. Thank you.
EDIT
It works for most of the countries I tested like this
offset = Math.abs(offset) so it always make it positive
You do not need to think about the offset. Moment can do that for you:
moment.utc("2018-10-03 05:55:51").local().format()
Also, keep in mind that when you asked for the offset in your code, you asked for the current offset, which may or may not be the offset in effect at the time you're converting. See "Offset != Time Zone" in the timezone tag wiki.
I'm trying to use moment.js to compare a date stored in the database (which is set to Europe/London timezone) against the current users time, taking into account their timezone.
I get a date string returned from the database and want to use the fromNow() function, as follows:
console.log(dbDate);
console.log(moment().format());
console.log(moment(dbDate).fromNow());
// DB stored time (Europe/London)
// 2017-09-26 06:56:26
// Current user time (timezone is Pacific Time / Los Angeles)
// 2017-09-25T23:59:03-07:00
// String output by fromNow() function, which should reflect the timezone difference but doesn't
// in 7 hours
I want the fromNow() string to take account the timezone difference and this should always be a time "ago" as opposed to in the future.
I'm probably missing something quite obvious with the library, so apologies in advance if this is very simple.
// get the current time so we know which offset to take
var now = moment.utc();
// get the zone offsets for this time, in minutes
var NewYork_tz_offset = moment.tz.zone("America/New_York").offset(now);
var MY_DATE = moment(dbDate);
// calculate the difference in hours
console.log((NewYork_tz_offset - MY_DATE) / 60);
Does this help your cause?
You have to use moment timezone, you can parse dbDate specifying "Europe/London" timezone using moment.tz:
The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.
Then you can use moment diff and fromNow.
Here a live example:
var dbDate = "2017-09-26 06:56:26";
var now = moment();
var momDbDate = moment.tz(dbDate, "Europe/London");
var pacificTime = moment("2017-09-25T23:59:03-07:00");
console.log(dbDate);
console.log(moment().format());
console.log(momDbDate.fromNow());
console.log(momDbDate.diff(now, 'hours'));
console.log(momDbDate.diff(pacificTime, 'hours'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"></script>
I am having trouble when converting from time zone to time zone using moment.js.
This is my code:
convertSelectedTimeZoneToClients() {
let timeZoneInfo = {
usersTimeZone: this.$rootScope.mtz.tz.guess(),
utcOffset: this.formData.timeZone.offset,
selectedDateTime: this.toJSONLocal(this.formData.sessionDate) + " " + this.formData.sessionTime
};
let utcTime = this.$rootScope.mtz.utc(timeZoneInfo.selectedDateTime).utcOffset(timeZoneInfo.utcOffset).format("YYYY-MM-DD HH:mm");
let con = this.$rootScope.mtz.tz(utcTime, timeZoneInfo.usersTimeZone).format();
return con;
}
The user picks date, time and time zone from drop downs on client page.
In timeZoneInfo object I am storing usersTimeZone (I want to be able to convert timezone that user selected on page and convert it to his local time zone).
For example user picks: 11/08/2016 01:30 and UTC+2 timezone and his timezone is UTC+1, than I want to show him in label: That is 11/08/2016 00:30 since UTC+1 is -1 hour comparing to UTC+2 timezone.
I store offsets for time zones in one object and those values are hard coded (utcOffset: this.formData.timeZone.offset).
Before I convert time form time zone to time zone I do this: get time zone -> convert to UTC time -> convert to user time zone.
What is happening is that utcTime variable has correct value. But when I pass that value and users time zone to .tz() function and using format() to get some readable value I get same time as utcTime like shown in picture:
I have read moment.js docs and by them this .tz().format() should do the work, but as you can see my result is: 2016-11-08T23:30:00+01:00.
So it gets that is should be incremented by 1 hour but how to accomplish to get: 2016-11-09T00:30 instead?
I have tried .format("YYYY-MM-DD HH:mm") as well same problem. When I use .local() function that should convert from utc time to specified time zone same problem is present.
Am I getting something wrongly? I am pretty sure that when you convert from 2016-11-08T23:30 UTC to UTC+1 it should be 2016-11-09T00:30, or one hour forward. Does someone sees something strange in this code?
I am given a spesific task to obtain the current time in a given format, and then convert said time to a Unix timestamp, but I'm feeling a little in over my head here.
After some research and creative googling, I came up with:
this.$el.find("#settime").val(moment((new Date).getTime()).format("DD.MM.YYYY HH:mm"));
Here the:
(moment((new Date).getTime()).format("DD.MM.YYYY HH:mm"))
...part being the central one.
Now, once the user sees the date in this format, he/she needs to be able to edit it, and feed it back to my application.
At this point, I need it converted.
Assume that a user who's using my application wants 04.08.2015 12:00, but their local time reads 04.08.2015 13:00, so they lower it by an hour and throw it back to my application.
How would I go about converting the updated time back to a timestamp? Note, that the time will always come in at that format (DD.MM.YYYY HH:mm).
You can get the timestamp with the following code
Math.floor(new Date().getTime() / 1000)
if you want to convert a datetime string to timestamp, then pass that string to Date() and call the getTime() method on it and divide the result by 1000.
so if the user provides August 4th 2015 13:13 then
Math.floor(new Date("08.04.2015 13:13").getTime() / 1000)
will give the timestamp corresponding to the given date 1438674180,
You will have to convert the modified date into MM.DD.YYYY format for this to work.
Refer this link to know more about Dates