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'));
Related
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.
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>
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;
}
I have a script in cloud code in parse.com. And I'm trying to use moment-timezone.js because I need to convert the timezone of my installation from string (ie "Europe/Paris") to a time offset from UTC (GMT).
My code so far imported from this question :
var moment = require('cloud/moment.js');
var tz = require('cloud/moment-timezone.js');
function toTimeZone(time, zone) {
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(time, format).tz(zone).format(format);
}
I have copied the latest moment.js and moment-timezone.js files from their website into my cloud directory.
Here is the error I get from log in cloud code :
Failed with: TypeError: Object Invalid date has no method 'tz'
at toTimeZone (main.js:6:30)
at query.find.success (main.js:19:17)
at Parse.js:2:10161
at e (Parse.js:2:8941)
at Parse.js:2:8390
at Array.forEach (native)
at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661)
at c.extend.resolve (Parse.js:2:8341)
at e (Parse.js:2:9076)
at Parse.js:2:8390
According to Parse's documentation https://www.parse.com/docs/js/guide#cloud-code-modules-moment
You can try this:
var moment = require('cloud/moment');
var tz = require('cloud/moment-timezone');
When I use toLocaleDateString in browser it returns
n = new Date()
n.toLocaleDateString()
"2/10/2013"
but in node.js the format is completely different
n = new Date()
> n.toLocaleDateString()
'Sunday, February 10, 2013'
How to get the browser's format (mm/dd/yy) in node.js?
For me, the solution was to install an additional module full-icu for node js
full-icu-npm
And after in package.json insert:
{"scripts":{"start":"node --icu-data-dir=node_modules/full-icu YOURAPP.js"}}
or
In the current version of Node.js v10.9.0 is Internationalization Support.
To control how ICU is used in Node.js, you can configure options are available during compilation.
If the small-icu option is used you need to provide ICU data at runtime:
the NODE_ICU_DATA environment variable:
env NODE_ICU_DATA=/some/directory node
the --icu-data-dir CLI parameter:
node --icu-data-dir=/some/directory
I also found this broken in node.JS. For example, in node console, type
new Date().toLocaleDateString('en-GB')
it still displays US format. Using Werner's method above, you can override default Date.toLocaleDateString() behavior for your locale:
Date.prototype.toLocaleDateString = function () {
return `${this.getDate()}/${this.getMonth() + 1}/${this.getFullYear()}`;
};
UPDATE: 2021-12-01
Starting with Node.js v13.0.0, releases are now built with default full-icu support. So this shouldn't be a problem with later releases.
in node.JS you can not get browser's format.
Node.JS runs on Server-side.
You have to do date formatting before displaying it in browser at your client side JS framework.
Date.prototype.toLocaleDateString = function () {
var d = new Date();
return (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
};