How to get hour from current time using dates.getHour - bixby

I am trying to get the current hour of the time which i try in this approach.
var currentTimeHour = dates.ZonedDateTime.now().getHour()
It did return value but it is zero while my time is 3pm. And i notice when the time turn to 4pm, the value return become one.
I did tried getMinute and getDay and it seems fine. Did i did anything wrong and is there anyway to get the hour of the current time?
Thanks.

dates.ZonedDateTime.now().getHour() is correct one to use.
Or you may want to use just dates.ZonedDateTime.now().time.hour
var myNow = dates.ZonedDateTime.now()
console.log('myNow', myNow)
var myHour = dates.ZonedDateTime.now().getHour()
console.log('myNow.getHour()', myHour)
Let's try the above code in JS file, and can see clearly the structure of now() in debug console. As the following screenshot.
First confirm the timezone is correct. IDE may change to a different timezone. This is the screenshot if I change IDE to Indiana timezone.
To confirm/change IDE timezone, use simulator -> User -> GPS/Clock override feature.

Related

How do I get the current time zone where my app resides

I've been having this issue for months and I've finally made some headway. I'm writing an app the sends me a message at specific times, 9 am and 9 pm eastern time. When I ran it locally it worked perfectly but when I deploy it, I get nothing. I was messing around and then I saw this Heroku Logs. My guess is that my app is located on a server that is in a different time zone and when this code below runs. The conditions are never met and nothing gets sent. My question now is, is there a way I can get the current time of and compare regardless of what time zone the server is located?
const sendMessage = require('./sms-api.js');
const send = () =>{
setInterval(()=>{
var x = new Date().toLocaleTimeString();
console.log(x);
if(x === '11:00:10 AM')
{
console.log('match');
return sendMessage('6178032176', 'Good Morning');
}
else if(x === '9:50:20 PM')
{
console.log('match');
sendMessage('6178032176', 'Good Evening');
}
},1000)
}
send();
When working with different timezones, it is better to work in UTC and then offset it according to required timezone.
Get the time in UTC and then offset it according to required timezone.
You can also use dedicated libraries like moment-timezone.
https://momentjs.com/timezone/docs/
Like Suyash said above, your best option is to work entirely in UTC, and only convert when displaying times to users. Rather than dealing with offsets, you can append your dates and times with a 'Z' to indicate they are universal.
The best way I've found to do that is with moment.js and moment-timezone.js. Here is an example of an implementation that will allow you to convert times and dates: https://github.com/aidanjrauscher/browser-timezone-conversions. These libraries also make it very convenient to convert any date or time related user input back from their local time zone to UTC.
thank you for your help. I ended up figuring it out. I used this instead const time = new Date().toLocaleTimeString('en-US', { timeZone: 'America/New_York' });.

Why ( new Date() ).toString() is so slow in Node.js?

I am playing a bit with Node.js. I've just started writing something new and it stuck me that my simple "console" app takes quite long to respond. This app loads a 5MB json file, turns it into an object but all that still does not take a significant amount of time. My further search (in a quite short and simple code) led me to conclusion that this single line:
this.generated_on = ( new Date() ).toString();
takes around 2.5s to execute. Further investigation made me understand even less. I've modified it to:
this.generated_on = new Date();
this.generated_on = this.generated_on.toString();
(with console.timeLogs in between) and line with toString() was the one that took over 2 seconds to execute. Then, I've modified the code once again:
this.generated_on = new Date('2019-02-04 20:00:00');
this.generated_on = this.generated_on.toString();
and results were other way around. toString() took only 2ms while creating Date object took over 2s.
Why is it so slow? Why so different results? Any faster way to get formatted current time string? (I don't care much about execution time for this project as it works offline, still it bugs me).
I think your development environment is off or something. I cannot tell you why your machine is running the code in a slow manner. I cannot replicate the problem you were saying.
I tried to benchmark the code you have above.
https://repl.it/#act/HotpinkFearfulActiveserverpages
Go here and try clicking on Run button at the top.
Here's the result I am seeing
// Case Togehter:
// this.generated_on = ( new Date() ).toString();
// Case Separately:
// this.generated_on = new Date();
// this.generated_on = this.generated_on.toString();
Together x 332,222 ops/sec ±7.75% (44 runs sampled)
Separtely x 313,162 ops/sec ±8.48% (43 runs sampled)
332,222 ops/sec means an operation took 1/332,222 seconds on average.
I suggest you to use node.js builtin module 'Performance hooks'.
You can find documentation here: https://nodejs.org/dist/latest-v11.x/docs/api/perf_hooks.html#perf_hooks_performance_mark_name
Mark each process from top to bottom and then print the metrics (in milliseconds), you will figure out the actual issue

Change the browser's Date/time value using chrome extension

I'm looking for a way to change the value returned from javascript's new Date() function.
In general - I'm looking for a way to write an extension that will give the user the ability to set his timezone (or diff from the time of his system's clock) without changing time/timezone on his computer.
I checked the chrome extension api but found nothing there. Will appreciate it if someone can point me at the right direction.
I have seen some solutions involving overriding javascript's getTime() method:
Date.prototype.getTime = function() { return [DATE + OFFSET] };
usage:
(new Date).getTime();
(source)
Apparently the time does not come from the browser but rather the operating system. So my suggestion would be to inject this javascript using a content script, and in your overriding function, you can offset the date/time by however much you would like.

Moment.js toDate does not always return a Date instance

Basically, I need to calculate the expiration date for a (very long-living) cookie, so I want to do now + 99 years. As this is way easier when using a library such as Moment.js than with native JavaScript, I am doing:
var expirationDate = moment().add('years', 99);
But I need a JavaScript Date object and call the toUTCString function on it to get the properly formatted string for the cookie. Hence I am doing:
var expirationDate = m().add('years', 99).toDate().toUTCString();
Now, something very strange happens. When I run this line in a Node.js shell, everything is fine. But if I run it from within a script, it fails. The error message is
TypeError: Object Mon, 24 Oct 2112 07:34:34 GMT has no method 'toUTCString'
which is correct as the thing returned by toDate is no instance of Date, but just a plain old object. Curiously enough, when I try the exact same thing in the Node.js REPL, toDate returns an instance of Date.
I run both, the REPL and the script, using Node.js 0.8.25, both on the same machine. Moment.js is version 2.3.1.
Any idea what might cause this issue?
You should just be able to do this in one step:
moment().add('years', 99).toISOString()
But I think the main problem was identified by dystroy in comments.
did you try something like:
expirationDate = new Date(moment().add('years', 99)); ?
This way, You will get back a date insteance, I tried MomentJs's .toDate() but it did not suite my needs.

Add Timestamp to File Name in Flash Media Server

Is there some way to dynamically name files published in Flash Media server.
Several clients in an application will be publishing to FMS. They may start and stop recording several times, and I would like to append a time stamp (format: yy-mm-dd-hh-mm-ss) to the file name in main.asc.
For example the following files might be created by clients 1 and 2 using the ns.publish(myclientName); command;
client1's first recording client1_2011-01-01-22-47-01.flv
client1's second recording client1_2011-01-01-22-54-55.flv
client2's first recording client2_2011-01-01-22-59-34.flv
client1's third recording client1_2011-01-01-22-04-12.flv
I don't want to use ns.publish(myClientName, "append");. There needs to be a separate file for each publish session.
The best I can come up with is to use File.creationTime and File.renameTo() on application.onUnpublish() to add the timestamp when publishing has ended, but it it wouldn't be tolerant of an unexpected server outage.
Edit: Unknown to me and in conflict with the documentation, the Date object in Flash Media Server is not the one we know and love. It has no properties. For example
var currentTime = new Date();
trace("CurrentTime: " +currentTime.time);
prints
CurrentTime: undefined
Running
for (var prop in currentTime)
trace(prop);
prints nothing.
I was surprised and frustrated after an hour or so to learn this. Hope it helps someone.
currentTime.valueOf() is timespan

Resources