So I'm doing some Ajax trickery in the front page, and in the DJango backend, I send a JS Object, using AJAX...
the format is: 'Tue Jan 28 2014 00:00:00 GMT-0800 (PST)'
So I'm trying to convert it to a Python object:
import datetime
81 if request.is_ajax():
82 datestr = request.POST['from_date']
83 date = datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S.%fZ").date()
84 message = date.__str__()
85 else:
86 message = "Not Ajax"
87
88 return HttpResponse(message)
However I'm getting the following error:
time data 'Tue Jan 28 2014 00:00:00 GMT-0800 (PST)' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
How could I fix that?
I'm looking forward a nicer solution that would avoid splitting and parsing the string ...
Given the format in the error message, then the client you can use ES5s Date.prototype.toISOString() to convert a Date object to an ISO 8601 string. You'll need a polyfill for browsers that don't have it.
Related
I'm trying to get the expiry date and time of an AWS S3 object. I'm using Node.JS/JavaScript.
I can get the object data using the AWS SDK and s3.headObject(params) command and from the response I can get the Expiration string:
expiry-date="Wed, 20 Jan 2021 00:00:00 GMT", rule-id="ExpireCachedAssets"
How can I parse the string to get a JavaScript date object and check how long until it expires?
If the output always comes in that way, you can use regex to retrieve the date:
"([^"]+)GMT"'
that would match everything from " to the GTM", which retrieves date it as:
new Date('expiry-date="Wed, 20 Jan 2021 00:00:00 GMT", rule-id="ExpireCachedAssets"'.match('"([^"]+)GMT"')[0]);
// Wed Jan 20 2021 03:00:00 GMT+0300 (GMT+03:00)
P.S. You might want to use getTime() to get a UNIX timestamp.
P.S. S. Consider using fallback in case there is no match found.
I tried to use monent.js to convert it
const newDateResult=moment(date).format('MM-DD-YYYY')
but the data type I got is a String
what I need is I want to convert it to MM/DD/YYYY and the data type still need to be "date"
Your moment code is on the right track, but the output of format() is always String. Try adding .toDate() instead to convert your result to a Date.
moment("Jul 02 2020 00:00:00 GMT+0800").toDate()
In my app, I am using MVC architecture. My controller returns data about the client such as birthdate. Client info is converted into a JSON object and passed to view.
Now the problem is my DateTime field is acting little strange. My date passed from the view is correct:
{29.07.1978 0:00:00}
After conversion to json it is:
/Date(270511200000)/
Now comes the strange part - it returns
28.07.1978
When I try to look for more detail by converting the json date to Date myself by this:
var date = new Date(parseInt(self.FormData.Person.BirthDate().substr(6)));
I get
Fri Jul 28 1978 23:00:00 GMT+0100
But when I enter some newer Date - for example, 1.1.2000, I get a correct date of 1.1.2000.
For some other dates, I'm getting
Fri Jul 28 1959 22:00:00 GMT+0200
Any idea why is this happening? My timezone is GMT+1.
Thanks a lot for any input :)
As per Jonas Wilms answer below - `
Cause summer time was introduced in your country in 1979
timeanddate.com/time/zone/czech-republic/prague
What fixed this for me was
if (birthYear <= 1979) {
if (self.FormData.Person.BirthDate() != null) {
self.FormData.Person.BirthDate(moment(self.FormData.Person.BirthDate()).utcOffset(2).format('DD.MM.YYYY'));
}
}
else {
if (self.FormData.Person.BirthDate() != null) {
self.FormData.Person.BirthDate(moment(self.FormData.Person.BirthDate()).format('DD.MM.YYYY'));
}
}
Why do I get such differing results with similarly formatted date strings when creating a new date?
CHROME (43.0.2357.134 m) console:
new Date('2014-12-25')
Wed Dec 24 2014 17:00:00 GMT-0700 (Mountain Standard Time)
[Chrome assumes Z with 00:00 (utc) and returns that local time]
new Date('2014-1-25')
Sat Jan 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
[What?! exact same format (I thought) but returns 25 instead of 24....see next...]
new Date('2014-01-25')
Fri Jan 24 2014 17:00:00 GMT-0700 (Mountain Standard Time)
[...oh, the leading 0 makes it use the logic it used in the first example]
new Date('2014/12/25')
Thu Dec 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
[using / instead of - results in what I believe most would expect(?): a local time
on the same date specified]
FIREFOX (39.0) console:
new Date('2014-12-25')
Date 2014-12-25T00:00:00.000Z
[different from Chrome]
new Date('2014-1-25')
Invalid Date
[not recognized in Firefox, unlike Chrome]
new Date('2014-01-25')
Date 2014-01-25T00:00:00.000Z
[different from Chrome]
new Date('2014/12/25')
Date 2014-12-25T07:00:00.000Z
The lesson seems to be: IF you're going to use strings in the Date constructor, make sure it's formatted correctly (per ECMAScript standard):
YYYY-MM-DDTHH:mm:ss.sssZ
CHROME:
new Date('2014-12-25T00:00:00.000-07:00')
Thu Dec 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
FIREFOX:
new Date('2014-12-25T00:00:00.000-07:00')
Date 2014-12-25T07:00:00.000Z
The ECMAScript standard says in 15.9.3.2
If Type(v) is String, then
Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2);
And in 15.9.4.2 it says:
The function first attempts to parse the format of the String according
to the rules called out in Date Time String Format (15.9.1.15). If the
String does not conform to that format the function may fall back to any
implementation-specific heuristics or implementation-specific date formats.
...which says to me, if you don't provide that exact format, Chrome and Firefox and everyone else can interpret the date string how they deem right. (note: there is some leeway on the format, for example, the value of an absent sss field is “000”. see section 15.9.1.15 for more details)
I have following Java Script (Json) date format
data.d1: "2015-03-26T16:00:00.0000000"
I execute the following
data.d1 = new Date(data.d1);
It gives the following outcome which is wrong to me.
Thu Mar 26 2015 20:00:00 GMT+0400 (Arabian Standard Time)
It should return
Thu Mar 26 2015 16:00:00 GMT+0400 (Arabian Standard Time)
Why there is 4 hour difference?
How i can get the same time (without 4 hours addition to me default time)?
Any hint please
p.s. i can get exact time back by using following line of code
data.d1.setHours(data.d1.getHours() - 4);
Is this the only way?
The 'T' in 2015-03-26T16:00:00.0000000 makes the Date constructor take UTC timezone into consideration. For you it's +4 hours, for me, for instance, it's +2 hours.
If you want the neutral time, you need to remove the 'T' from the string and you'll get the desired result: 2015-03-26 16:00:00.0000000
Fiddle
See this question if you want a pure JS solution without altering your string, it will work I've tested it.