Using moment to define time in an UTC-based cloud server? - javascript

My application heavily relines on time being on pacific time
If I deploy my application on Google App Engine (US-WEST region), the time that is calculated by the cloud app engine is different than my local machine.
I've tried using Moment-Timezone to make it force the cloud server machine to use los angeles time format however its still giving me a different time than my local machine.
Is there a way to use moment so that the time values are consistent no matter where its deployed? Or did I implement the moment-time zone incorrectly?
const test = moment(new Date(filteredTimeLeft))
const endTime = test.tz('America/Los_Angeles').unix();
console.log('endTime', endTime)
const convertSec = Number(coin.seconds);
console.log('convertSec', convertSec)
const newDate = moment(new Date())
const calcTime = endTime - convertSec - newDate.tz('America/Los_Angeles').unix();

Not sure how you came with usin the plain JS new Date along with moment, nor the use of unix().
And I don't know what coin.seconds can be...
Anyway, here an example showing
the plain JS date
the UTC time
your local time
the Los Angeles time
the New York time
let js_date = new Date();
console.log("Plain JS date", js_date);
let moment_UTC = moment().utc();
console.log("UTC time", moment_UTC.format("hh:mm:ss"));
let moment_local = moment();
console.log("Your local time", moment_local.format("hh:mm:ss"));
let moment_Los_Angeles = moment().tz('America/Los_Angeles');
console.log("Los Angeles", moment_Los_Angeles.format("hh:mm:ss"));
let moment_New_York = moment().tz('America/New_York');
console.log("New York", moment_New_York.format("hh:mm:ss"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>

Related

Date miscalculation on aws EBS server. It's running perfectly fine on local

There is always a miscalculation when deployed on aws server.
Context:
All the dates stored in mongoDB are in UTC format. And I want to convert the dates to IST before I export them in excel.
My code works perfectly fine on my local machine, but when deployed on server, it fails.
Here is an example that will help understand the issue.
const myDate = new Date('2022-12-21T18:41:18.384+00:00');
// The value of convertedDate should be 2022/12/21
// But, it for some reason returns - 2022/12/22
const convertedDate = myDate.toLocaleDateString('en-GB');
I also tried to user external library date-fns to handle the issue. Following is the sample code:
const zonedDate = utcToZonedTime(myDate, 'Asia/Kolkata');
// this is also - 2022/12/22
const convertedDate = formatInTimeZone(zonedDate, 'Asia/Kolkata', 'yyyy-MM-dd');
);
Another variation for date-fns that I tried was:
const zonedDate = utcToZonedTime(
new Date(myDate.valueOf() + myDate.getTimezoneOffset() * 60 * 1000),
'Asia/Kolkata',
);
// this is also - 2022/12/22
const convertedDate = formatInTimeZone(zonedDate, 'Asia/Kolkata', 'yyyy-MM-dd');
Please note that locally, everything works perfectly fine.
I also tried to use external library date-fns to handle the issue.
It seems you converted back-and-forth, or even converted twice (for double the timezone offset?). You should not need to use utcToZonedTime at all. Just pass your original myDate timestamp into formatInTimeZone:
const convertedDate = formatInTimeZone(myDate, 'Asia/Kolkata', 'yyyy-MM-dd');
locally, everything works perfectly fine
Change your system timezone to be that of the server (most likely UTC) and you should be able to reproduce the issue. For node.js, start it with the TZ environment variable.

Why is Google App Engine parsing different time?

My application heavily relies on time calculation and I read online that GCP App Engine uses UTC time zone.
So I made these calculations and on my local dev, the calcTime is totally different than App Engine, App Engine is giving me a big -negative number I'm not sure why??
const endTime = Math.floor(new Date(filteredTimeLeft).getTime() / 1000);
const convertSec = Number(coin.seconds);
const dateNow = Math.floor(Date.now() / 1000);
const calcTime = endTime - convertSec - dateNow;
Isn't my code suppose to work universally , independent where the server is located at?

Make redis cache expire at certain time everyday

I am using node-redis. I have a cron job that updates my db and I have redis cache that caches the db response.
The problem I'm having is that my cron job runs everyday at 12am, however I can only set redis cache to expire in x seconds from now. Is there a way to make node-redis cache expire everyday at 12am exactly. Thanks.
Code:
const saveResult = await SET_CACHE_ASYNC('cacheData', response, 'EX', 15);
yes, you can use https://redis.io/commands/expireat command, if you use https://www.npmjs.com/package/redis package as redis driver, code will be like this
const redis = require('redis')
const client = redis.createClient();
const when = (Date.now()+24*60*60*1000) / 1000;
client.expireat('cacheData', when, function(error){....};
``
Recently I had the same problem with an application. My work around was creating a new timespan based on the time difference between my set and expiration time. Here is my code:
private TimeSpan GetTimeSpanUntilNextDay(int hour)
=> new DateTime(DateTime.Now.Date.AddDays(1).Ticks).AddHours(hour) - DateTime.Now;
Using the stack exchange lib for a 6 AM absolute expirition time the code looks like so:
public async Task<bool> SetEntranceAsync(string key, MYTYPE unserializedObject)
{
var db = _multiplexer.GetDatabase();
var jsonValue = JsonConvert.SerializeObject(unserializedObject);
return await db.StringSetAsync(key, jsonValue, GetTimeSpanUntilNextDay(6));
}
I used C# but you should be able to do the trick in any language.

How to get server date using JavaScript?

I am using AngularJS client-side application and working with date and time. My problem is that local system date anyone can change, to protect this I want to use server date without any get or post request. Is there any way to get server date using JavaScript?
If you are running JavaScript at the client-side, the only way to find the server time is asking the server what is the current time. There is no magic here. You need to make a request to the server.
Options:
Use AJAX or Fetch.
If the HTML page is rendered in the server, you can write the current time during the page render and send it to client.
Please, note that it is not possible to have a precise time of the server due to network delays, but you can get pretty close using the code from this answer (modified):
var offset = 0;
function calcOffset() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "https://stackoverflow.com/", false);
xmlhttp.send();
var dateStr = xmlhttp.getResponseHeader('Date');
var serverTimeMillisGMT = Date.parse(new Date(Date.parse(dateStr)).toUTCString());
var localMillisUTC = Date.parse(new Date().toUTCString());
offset = serverTimeMillisGMT - localMillisUTC;
}
function getServerTime() {
var date = new Date();
date.setTime(date.getTime() + offset);
return date;
}

Parsing Javascript Date to java.utils.date.Date

I have a service provide dates and i need process this in mi Android App
In my Node server:
server.send({'date': new Date() });
In my android app:
Date date = new Date(jsonObject.getString('date'));
this throws me a java.lang.IllegalArgumentException error
if a print in the android Log i get 2013-02-21T17:55:27.885Z how to handle this format
Store your Date as a long using
date.getTime()
making your calls look like this:
server.send({'date': new Date().getTime() });
and
Date date = new Date(jsonObject.getString('date'));

Categories

Resources