2011-11-16 12 views
11

मैं dhtmlxscheduler के साथ काम कर रहा हूं और मैं प्रसंस्करण के लिए django सर्वर पर तिथियां भेज रहा हूं।जेएस डेट ऑब्जेक्ट पाइथन डेटाटाइम

Dhtmlxscheduler मेरा पीछा तारीख वस्तु के साथ प्रदान करता है, नीचे दूसरी पंक्ति से शुरू प्रदान की विधि:

end_date: Sat Nov 19 2011 01:00:00 GMT-0500 (EST) 
__proto__: Invalid Date 
constructor: function Date() { [native code] } 
getDate: function getDate() { [native code] } 
getDay: function getDay() { [native code] } 
getFullYear: function getFullYear() { [native code] } 
getHours: function getHours() { [native code] } 
getMilliseconds: function getMilliseconds() { [native code] } 
getMinutes: function getMinutes() { [native code] } 
getMonth: function getMonth() { [native code] } 
getSeconds: function getSeconds() { [native code] } 
getTime: function getTime() { [native code] } 
getTimezoneOffset: function getTimezoneOffset() { [native code] } 
getUTCDate: function getUTCDate() { [native code] } 
getUTCDay: function getUTCDay() { [native code] } 
getUTCFullYear: function getUTCFullYear() { [native code] } 
getUTCHours: function getUTCHours() { [native code] } 
getUTCMilliseconds: function getUTCMilliseconds() { [native code] } 
getUTCMinutes: function getUTCMinutes() { [native code] } 
getUTCMonth: function getUTCMonth() { [native code] } 
getUTCSeconds: function getUTCSeconds() { [native code] } 
getYear: function getYear() { [native code] } 
setDate: function setDate() { [native code] } 
setFullYear: function setFullYear() { [native code] } 
setHours: function setHours() { [native code] } 
setMilliseconds: function setMilliseconds() { [native code] } 
setMinutes: function setMinutes() { [native code] } 
setMonth: function setMonth() { [native code] } 
setSeconds: function setSeconds() { [native code] } 
setTime: function setTime() { [native code] } 
setUTCDate: function setUTCDate() { [native code] } 
setUTCFullYear: function setUTCFullYear() { [native code] } 
setUTCHours: function setUTCHours() { [native code] } 
setUTCMilliseconds: function setUTCMilliseconds() { [native code] } 
setUTCMinutes: function setUTCMinutes() { [native code] } 
setUTCMonth: function setUTCMonth() { [native code] } 
setUTCSeconds: function setUTCSeconds() { [native code] } 
setYear: function setYear() { [native code] } 
toDateString: function toDateString() { [native code] } 
toGMTString: function toGMTString() { [native code] } 
toISOString: function toISOString() { [native code] } 
toJSON: function toJSON() { [native code] } 
toLocaleDateString: function toLocaleDateString() { [native code] } 
toLocaleString: function toLocaleString() { [native code] } 
toLocaleTimeString: function toLocaleTimeString() { [native code] } 
toString: function toString() { [native code] } 
toTimeString: function toTimeString() { [native code] } 
toUTCString: function toUTCString() { [native code] } 
valueOf: function valueOf() { [native code] } 
__proto__: Object 

इन toString तरीकों में से एक को चुनने के लिए और फिर अजगर सर्वर पर पार्स करने के लिए सबसे आसान तरीका क्या है एक पायथन डेटाटाइम ऑब्जेक्ट बनाने के लिए datetime.strptime() का उपयोग कर पक्ष?

Sat Nov 19 2011 00:00:00 GMT-0500 (EST) 

अलग स्वरूप निर्देशों की कोशिश कर रहा असफल साबित होता है:

सरल toString विधि मुझे प्रारूप में एक datetime देता है।

अर्थात्:

datetime.strptime("Sat Nov 19 2011 00:00:00 GMT-0500 (EST)", "%a %b %d %Y %H:%M:%S %Z") 
---> unconverted data remains: -0500 (EST) 

और:

datetime.strptime("Sat Nov 19 2011 00:00:00 GMT-0500 (EST)", "%a %b %d %Y %H:%M:%S %z") 
---> ValueError: 'z' is a bad directive in format '%a %b %d %Y %H:%M:%S %z' 

उत्तर

12

toUTCString() देता है:

"Tue, 22 Nov 2011 06:00:00 GMT" 

और उसके साथ parsible है:

datetime.strptime("Tue, 22 Nov 2011 06:00:00 GMT", "%a, %d %b %Y %H:%M:%S %Z") 
5

ऐसा लगता है कि आपको सामान्य जावास्क्रिप्ट दिनांक वस्तु की तरह कुछ मिल रहा है। इस मामले में, टाइमस्टैम्प प्राप्त करने के लिए शायद सबसे आसान तरीका getTime विधि का उपयोग करना है। इसे 1321463229215 जैसे कुछ वापस करना चाहिए, जो कि मिलीसेकंड में सिर्फ एक टाइमस्टैम्प है।

datetime के fromtimestamp उम्मीद सेकंड में समय स्टांप, तो बस 1000.0 द्वारा कि टाइमस्टैम्प विभाजित और तुम जाना

from datetime import datetime 
datetime.fromtimestamp(1321463229215/1000.0) 
+2

दुर्भाग्यवश यह तरीका सेकंड के बाद मिलीसेकंड इकाइयों को लौटाता है। यानी: घंटा: मिनट: सेकंड: मिलीसेकंड। – TheOne

+1

getTCMilliseconds को पाने के लिए NTUTRMilliseconds को बदलने के लिए एनटी 3 आरपी की पोस्ट में एक संपादन जोड़ा गया और टाइमस्टैम्प को 1000.0 तक विभाजित किया गया क्योंकि सेप्टिमस्टैम्प सेकंड में टाइमस्टैंप की अपेक्षा करता है, मिलीसेकंड नहीं। – Andrew

1

उपयोग करने का प्रयास अच्छा कर रहे हैं अजगर-dateutil (अजगर-dateutil स्थापित पिप)

import dateutil.parser as dt 
date_time_obj = dt.parse("Tue, 22 Nov 2011 06:00:00 GMT") 

date_time_obj एक datetime वस्तु हो जाएगा

संबंधित मुद्दे