I am trying to figure out how to get current time in different time zones but below method does not return anything
var timeUpdate = setInterval(function () {
var currentTime = moment().format()
var GMT_current_time = currentTime.tz("Europe/London").format("HH:mm DD MMM");
$("#GMT_display_time").text(GMT_current_time);
}, 1000);
The output of the format function is a string. The tz function works on a moment object.
moment().tz("Europe/London")
Returns current time with format.
moment.tz(moment(), 'Asia/Karachi').format('DD/MM/YYYY HH:mm')
Related
I can't seem to figure out how to properly subtract two times that I've formatted using this line of code:
var moment = require('moment');
var moment = require('moment-timezone');
moment();
moment().tz("America/New_York");
clockedIn = moment().tz("America/New_York").format('HH:mm A');
clockedOut = moment().tz("America/New_York").format('HH:mm A');
From what I've researched, I can see that using the .format method transforms the time into a string. How would I be able to retain my local time variable and subtract it from another one? I am attempting to build a time punch bot and I have the clockIn/Out routed to an interactive menu with buttons. I am new to this and would appreciate any help/insight.
Not sure if I understood correctly, but just don't format the data until you actually output it.
var clockedIn = moment().tz("America/New_York");
console.log('In: ', clockedIn.format('HH:mm A'));
setTimeout(function() {
var clockedOut = moment().tz("America/New_York");
console.log('Out: ', clockedOut.format('HH:mm A'));
var difference = clockedOut - clockedIn; // milliseconds
console.log('Time (secs): ', difference / 1000);
}, 2000);
I need help with setting my timeout for the function. I'm trying to set the timeout for a given date and time but my conversion of them to milliseconds is not working.
Here's my code. Please help.
<script>
var ref = firebase.database().ref().child("Message");
var newRef = ref.child("20161227125916539")
newRef.on('value',function(snap){
heading.innerText =snap.child("message").val();
});
ref.on("child_added",function(snap){
var date = snap.child("date").val();
var time = snap.child("time").val();
var type = snap.child("type").val();
var venue = snap.child("venue").val();
var route = snap.child("route").val();
var message = snap.child("message").val();
date = date + time;
date = date.getTime();
var now = new Date();
now = now.getTime();
set = date - now;
var explode = function(){
alert("Boom!");
};
setTimeout(explode, 2000);
});
</script>
You need to parse the date using new Date().
As you said, the value of date is "2016-12-27" and the value of time is "15:30", so while concatenating them, you also need an extra space. Something like:
date = date + " " + time;
var someDate = new Date(date);
var now = new Date();
var diffInMillis = now - someDate
var explode = function(){
alert ("Boom!");
}
setTimeout(explode, diffInMillis);
dateobj=new Date(datestring);
timeinmilliseconds=dateobj.getTime();
//by the way, may check the browsers console if sth is not working:
datestring.getTime();// error:undefined function
Youre calling the getTime function on a string. You need to convert it into a time obj first. Be aware of the right String format. There are good resources online.
The better Way:
A timeout is killed when the browser is reloaded. Thats bad. It would be better to store the time, and regularily check if the time is reached. That would survive reloads, crashes, shutdowns etc:
function set(timestring){
localStorage.setItem("timer",new Date(timestring).getTime());//store timer
check();//start checking
}
function check(){
if(var await=localStorage.getItem("timer")){//if timer is set
var now=new Date().getTime()
if(await<=now){//time reached, or reached in the past
alert("Yay, timer finished");
}else{//not reached yet
console.log(await-now+" left");//log the time left
setTimeout(check,1000);//check again in a scond
}}
window.onload=check;// browser started, check for an existing timer
Use like this:
set("28-12-2016 12:30");
I have a start date defined in a database and I need to know when the start date is greater then the current time. I've tried using setInterval, but I can't access the updated time outside the setInterval function.
This is what I need to compare the current time to the database variable:
if(startDate > now) {
var status = 'pre';
}
I've tried using setInterval as follows, but I can't access the updated (current time) outside of the setInterval function.
setInterval(function(){
now = moment(new Date()).format();
console.log('time1 ', now); // this works
}, 1000);
console.log('time2 ', now); // this doesn't
What am I doing wrong?
Try this..
var now = new Date();
setInterval(function(){
now = new Date();
console.log('time1 ', now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
}, 1000);
console.log('time2 ', now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
});
I ended up using react-interval https://github.com/nkbt/react-interval to accomplish this.
I am quite low at javascript. I started to use moment.js library for my project to make javascript code to work by time & date, which always aligned by specific time zone. But I am struggling to understand how access different parts of my variable. My code:
var ItalyZone = "Europe/Rome";
var currentTime= moment().tz(ItalyZone).format();
alert(currentTime.hours()); //this is not working....
How can access only hours/minutes of that variable "currenTime" ?
How to set new hours for that variable "currentTime" ?
Using simple javascript Date() function I could do simply currentTime.getHours() / currentTime.setHours(), but how should I do using moment.js ???
Why use format when you just want the hours, return a date object instead
var ItalyZone = "Europe/Rome";
var currentTime = moment().tz(ItalyZone).toDate(); // return JS date object
var hours = currentTime.getHours()
You can use the get-set function to do this
moment().minute(Number);
moment().minute(); // Number
moment().minutes(Number);
moment().minutes(); // Number
Gets or sets the minutes.
You can use date format to get any part of date time:
var ItalyZone = "Europe/Rome";
moment().locale(ItalyZone).format("yyyy"); // year
moment().locale(ItalyZone).format("MM"); // month
moment().locale(ItalyZone).format("DD"); // day
moment().locale(ItalyZone).format("hh"); // hours
moment().locale(ItalyZone).format("mm"); // minutes
moment().locale(ItalyZone).format("ss"); // seconts
console.log(moment().locale(ItalyZone).format("YYYY MM DD hh mm ss"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
I have the following data:
var currentTime: 2013-07-11 15:55:36+00:00
var currentTimezone: Africa/Asmera
I need a way to convert the currentTime in UTC to a new time based on currentTimezone.
I've looked into Timezone.js and I'm having trouble implementing it (the directions on the site are a little ambiguous)
The code for the function I'm intending on using is included. Thanks :)
<script>
$("#storeTime").click(function(){
storeCurrentTime();
})
$("#getTime").click(function(){
retrieveTime();
})
$("#storeTimezone").click(function(){
var yourTimezone = $('#timezone-select').find(":selected").text();
tz = yourTimezone.toString();
storeCurrentTimezone(tz);
})
$("#convertTime").click(function(){
//get the most recent UTC time, clean it up
var currentTime = $('#RetrievedTime').html();
currentTime = currentTime.split(": ")[1];
$('#convertedTime').html("Converted Time: " + currentTime);
//get the saved timezone
var currentTimezone = $('#storedTimezone').html();
})
</script>
You're going to need to know the timezone offset, so some sort of dictionary with strings to numbers.
// assuming your dictionary says 3 hours is the difference just for example.
var timezoneDiff = 3;
Then you can just make a new time like this
// Assuming you have the proper Date string format in your date field.
var currentDate = new Date(currentTime);
// Then just simply make a new date.
var newDate = new Date(currentDate.getTime() + 60 * 1000 * timezoneDiff);
Update
I've written a javascript helper for this which you can find at:
http://heuuuuth.com/projects/OlsonTZConverter.js
I pulled the timezone data from the wikipedia page https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Usage is as follows once included the script.
var offset = OlsonTZConverter.GetUTCOffset("Africa/Asmera");
or if there is Daylight Savings in effect:
var offset = OlsonTZConverter.GetUTCOffset("Africa/Asmera",true);
These will throw if you pass an invalid timezone, but you can check if a timezone is valid with:
var isValid = OlsonTZConverter.Contains("Africa/Asmera");
or just look at the entire dictionary with:
var tzDict = OlsonTZConverter.ListAllTimezones();
Hope this maybe saves someone some time sometime :).