Hello I am using Twitter REST API to call the home_timeline for timelines.
But I found that the object it returned had the created_at with GMT+0 timezone, how can I adjust the timezone to the user's local timezone for example GMT+8?
Here is the original data with GMT+0 from Twitter API:
Wed May 04 16:23:13 +0000 2016
what's the expected output format?
you can try:
var d = new Date('Wed May 04 16:23:13 +0000 2016');
var localeTime = d.toLocaleString();
console.log(localeTime);
if you expect a specific output format, you can use http://momentjs.com/timezone/ , to change both the timezone and format
I'm highly recommend to use MomentJS timezone library.
Furthermore, I'm recommend not to use GMT+8 label for time shifting, and use, for example, "Asia/Shanghai" instead. I would save hours to debug for you.
So, code barebone look like:
let moment = require("moment-timezone");
moment("Wed May 04 16:23:13 +0000 2016").tz("Asia/Shanghai").format('YYYYMMDD hh:mm:ss')
Output would be something like "20160505 12:23:13".
Thanks!
Related
I am trying to use luxon to generate a new date using a timezone. This is my code:
var luxon = require('luxon');
luxon.Settings.defaultZoneName = 'UTC+4';
var date = luxon.DateTime.local();
console.log(date);
var now = new Date(date.ts);
console.log(now.toString());
And this is the console:
DateTime {
ts: 2018-09-13T13:09:45.333+04:00,
zone: UTC+4,
locale: en-US }
Thu Sep 13 2018 11:09:45 GMT+0200 (CEST)
But if I try to access the ts property like so
var date = luxon.DateTime.local();
console.log(date.ts); // here
var now = new Date(date.ts);
console.log(now.toString());
I get this in the console:
1536830052009
Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)
Why is that? Is it doing some kind of math in the background? Also it turns out this date.ts is just ignoring my timezone. How can I fix that?
First 1536830052009, This is your time in milliseconds,
new Date(1536830052009)
// output Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)
You may want to check your timezone with getTimezoneOffset()
Returns the time difference between UTC time and local time, in minutes
Many people use moment.js to play with Date, I know it is not in your question but maybe you could find some usefull things
ts is not a public property and you shouldn't use it. Luxon does all sorts of tricks under the covers to get the math right. If you want the timestamp, just use date.toMillis(). If you want a JS Date, use date.toJSDate().
Two other important things to know:
It's not ignoring your zone. The zone doesn't change the time. It's more like metadata about a time that affects how we display it. The Luxon docs cover this a bit. You shouldn't expect to extract a different timestamp by fiddling with the zone. Now is always now.
Remember that the native Date object doesn't support timezones other than your local one. So anytime you convert from a Luxon object to a native Date, that information is lost. The time itself will be the same (meaning, it will represent the same millisecond), but it will express it in the local time.
I using django model forms to submit data to the database.
I use JavaScript to auto-fill the form with the following
document.getElementById('id_date_received').value = Date();
This outputs: Mon Feb 06 2017 11:39:05 GMT+0000 (GMT)
while django's models.DateTimeField expects: 2017-02-06 11:39
How do i convert: Mon Feb 06 2017 11:39:05 GMT+0000 (GMT) to 2017-02-06 11:39
Thanks
IMO, the best solution would be using unix timestamps, because you can avoid all complex stuff connected with timezones and time parsing.
JS:
js_date = new Date('2012.08.10');
// getTime() returns milliseconds from the UNIX epoch,
// so divide it by 1000 to get the seconds representation.
js_timestamp = js_date.getTime() / 1000;
Python:
python_date = datetime.datetime.fromtimestamp(js_timestamp)
You should consider use Moment.js, it's the easiest javascript library to manipulate dates and timezone formats.
So the code would by something like this:
moment(YOUR_DATE_VARIABLE).format('YYYY-MM-DD HH:mm'); // 2017-02-06 11:39
Hope this help you.
You can set the pattern of date in your model form that accept particular format.
input_formats=[list of datetime patterns that you want to accept]
This is a bit of a long-winded solution but this should work to convert Date to django date time
I first convert the Date to a string by cast
(String(date_var))
then when I receive the API call I convert it using this command
datetime.datetime.strptime(",".join(original_time[:original_time.find("(")-1].split(" ")).replace("GMT",""), '%a,%B,%d,%Y,%H:%M:%S,%z')
I would recommend preserving the timezone as different servers can be in different timezones which can screw up your dates!
Hello All, I have time in milliseconds and I want convert it into "dd/mm/yyyy hh:mm:ss"
I have tried the following code in javascript but I failed to do it, What I get is "Thu Jul 07 2016 16:22:10 GMT+0530 (IST)" this full timestamp, Code I tried is as follows:
self.users[i].lastLoggedIn = (new Date(self.users[i].lastLoggedIn)).toString("mm/dd/yyyy hh:mm:ss");
Where am I going wrong? for reference I have attached one screen shot of the same. Thank you..!!!
I suggest using moment.js for handling dates in Javascript. It is available for browsers, server-side engines and so on.
moment(self.users[i].lastLoggedIn).format("mm/dd/yyyy hh:mm:ss")
Hello I have an object in js with a field date. I try to stringify it at an ajax request but the result is inconsistent. After stringify the new object is one day earlier.
To be more specific this is the code on my file:
console.log(reservation.checkin);
console.log( JSON.stringify(reservation.checkin));
And this is the outcome:
Thu Jan 01 2015 00:00:00 GMT+0200 (EET)
"2014-12-31T22:00:00.000Z"
Am I doing something wrong? Is that output what it should be? Thx in advance!
edit: From an answer below it seems that it is at different timezone. What is the correct way to stringify this date?
It's not changing the date, just showing it in another timezone (UTC / GMT)
GMT+0200 (EET) means 2 hours differenece with UTC / GMT
so that's exaclty what you see in the result.
it depends a bit on the purpose. If you want ot post this in another API, it should work fine (presuming, the api uses timezone standards), in case you want to just show it in a gui... why use the json stringify...
I am not going to do all the math for you, i suggest you know why now, so just google: 'javascript format timezone' or something like that.
e.g:
Convert date to another timezone in JavaScript
Well, it is timezone - your date is GMT +2:00,
and after applying Stringify you get UTC.
You may want to check Date method .getTimezoneOffset() and maybe update the date
JSON.stringify is calling Date.toJSON() to convert the date.
See The "right" JSON date format.
I am displaying a date time object in a table however for some reason in IE it display as None or Invalid Date is there something wrong with my format or is there an easy way for making this more readable such as mm/dd/yyyy HH:MM
this is what displays in chrome:
Mon Nov 28 2011 16:00:00 GMT-0500 (EST)
This is being converted from a Unix timestamp to that output in an API layer.
Probably the creation of the Date object fails, because the new Date() constructor accepts just some limited, implementation-dependent set of date strings.
You can use the Globalize library to deal with such issues, even if no localization in the usual sense is involved—but dealing with different string presentations of dates as is localization of a kind. It first looks a bit messy (it takes some time to dig into it—my book “Going Global with JavaScript and Globalize.js” contains a more readable description of it, with many examples), and it’s far from perfect, but it’s very useful.
If you know that your timestamp data is of some known exact format, you can parse it easily and then output it according to your own format descriptor. Assuming, for the sake of definiteness, that the format is the one exemplified with
Mon Nov 28 2011 16:00:00 GMT-0500 (EST)
(I know it’s an output format you mentioned, but I just use it as an example), you would first do simple string operation to discard the “GMT” and “(EST)” part (Globalize cannot currently handle them), producing e.g.
Mon Nov 28 2011 16:00:00 -05:00
and then you would just use code like the following:
var foo = Globalize.parseDate(timestamp,'ddd MMM d yyyy HH:mm:ss zzz');
var out = Globalize.format(foo,'MM/dd/yyyy HH:MM');
document.write(out);
just make your own method to format the date as string so you pass all problems with diffrent browsers and plateforms
I suspect Chrome is being helpful here and calling the .toString() method for you.
The Date object has several methods for formatting string output. See the w3schools reference page for examples.