2012-01-20 44 views
15

我有一個Json時間戳,我想使用javascript將其轉換爲簡單的日期時間格式。將json時間戳轉換爲javascript中的正常日期和時間

我需要以下列格式的日期和時間:DD-MM-YYYY小時:mn個

這裏是從i希望提取時間戳的示例JSON日期: 「時間戳」:1326439500

{ 
    "count": 2, 
    "d": [ 
     { 
     "title": "Apple iPhone 4S Sale Cancelled in Beijing Amid Chaos (Design You Trust)", 
     "description": "Advertise here with BSA Apple cancelled its scheduled sale of iPhone 4S in one of its stores in China’s capital Beijing on January 13. Crowds outside the store in the Sanlitun district were waiting on queues overnight. There were incidents of scuffle between shoppers and the store’s security staff when shoppers, hundreds of them, were told that the sales [...]Source : Design You TrustExplore : iPhone, iPhone 4, Phone", 
     "link": "http://wik.io/info/US/309201303", 
     "timestamp": 1326439500, 
     "image": null, 
     "embed": null, 
     "language": null, 
     "user": null, 
     "user_image": null, 
     "user_link": null, 
     "user_id": null, 
     "geo": null, 
     "source": "wikio", 
     "favicon": "http://wikio.com/favicon.ico", 
     "type": "blogs", 
     "domain": "wik.io", 
     "id": "2388575404943858468" 
     }, 
     { 
     "title": "Apple to halt sales of iPhone 4S in China (Fame Dubai Blog)", 
     "description": "SHANGHAI – Apple Inc said on Friday it will stop selling its latest iPhone in its retail stores in Beijing and Shanghai to ensure the safety of its customers and employees. Go to SourceSource : Fame Dubai BlogExplore : iPhone, iPhone 4, Phone", 
     "link": "http://wik.io/info/US/309198933", 
     "timestamp": 1326439320, 
     "image": null, 
     "embed": null, 
     "language": null, 
     "user": null, 
     "user_image": null, 
     "user_link": null, 
     "user_id": null, 
     "geo": null, 
     "source": "wikio", 
     "favicon": "http://wikio.com/favicon.ico", 
     "type": "blogs", 
     "domain": "wik.io", 
     "id": "16209851193593872066" 
     } 
    ] 
} 
+1

var date = new Date(timestamp)var hours = date.getHours(); //來自時間戳的分鐘部分 var minutes = date.getMinutes(); //秒時間戳部分 var seconds = date.getSeconds(); //將以10:30:23格式顯示時間 var formattedTime =小時+':'+分鐘+':'+秒; –

+0

@RajatSinghal只會給出時間不是嗎? – praneybehl

+0

你可以從日期對象中獲取日期,年份...而不是gethours使用getYear ... –

回答

20

日期以自紀元以​​來的毫秒數返回。下面的代碼創建一個JS日期對象:

var d = new Date(1245398693390); 
var formattedDate = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear(); 
var hours = (d.getHours() < 10) ? "0" + d.getHours() : d.getHours(); 
var minutes = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes(); 
var formattedTime = hours + ":" + minutes; 

formattedDate = formattedDate + " " + formattedTime; 

這裏的a working fiddle

+0

感謝隊友的一個簡單例子。但是,這給了我整個: 2009-06-19T08:04:53.390Z 我想要的東西像dd-mm-yyyy hr:mn – praneybehl

+0

@praneybehl,我誤解了。你只是想要時間? –

+0

no mate我需要這種格式的日期時間:dd-mm-yyyy hr:mn – praneybehl

2
<script> 
var timestamp=1326439320; 
var date=new Date(timestamp); 
var hours = date.getHours(); // minutes part from the timestamp 
var minutes = date.getMinutes(); // seconds part from the timestamp 
var seconds = date.getSeconds(); // will display time in 10:30:23 format 
var formattedTime = hours + ':' + minutes + ':' + seconds; 
alert(formattedTime); 
</script> 
4

擴展Date的原型,包括像這樣的格式函數(或者尋找或創造你自己的):

Date.prototype.format = function (formatString) { 
    // Returns a formatted date string 
    var month = this.getMonth() + 1, 
     day = this.getDate(), 
     year = this.getFullYear(), 
     hours24 = this.getHours(), 
     hours = (hours24 === 0 ? 12 : hours24 > 12 ? hours24 - 12 : hours24), 
     meridiem = hours24 >= 12 ? "PM" : "AM", 
     minutes = this.getMinutes(), 
     seconds = this.getSeconds(); 

    return formatString.replace(/(MM)/g, month.padLeft(2, '0')) 
     .replace(/(M)/g, month) 
     .replace(/(dd)/g, day.padLeft(2, '0')) 
     .replace(/(d)/g, day) 
     .replace(/(yyyy)/ig, year) 
     .replace(/(yy)/ig, year.toString().substring(2, 4)) 
     .replace(/(hh)/g, hours.padLeft(2, '0')) 
     .replace(/(h)/g, hours) 
     .replace(/(HH)/g, hours24.padLeft(2, '0')) 
     .replace(/(H)/g, hours24) 
     .replace(/(mm)/g, minutes.padLeft(2, '0')) 
     .replace(/(m)/g, minutes) 
     .replace(/(ss)/g, seconds.padLeft(2, '0')) 
     .replace(/(s)/g, seconds) 
     .replace(/(tt)/g, meridiem.toLowerCase()) 
     .replace(/(TT)/g, meridiem); 
}; 

然後,時間戳轉換成所需的格式,dd-mm-yyyy hr:mn(如前所述您的評論),你會做以下幾點:

var dateString = new Date(timestamp).format("dd-MM-yyyy hh:mm"); 

[編輯]這裏是伴隨墊功能:

Number.prototype.padLeft = function (width, padChar) { 
    // Returns a padded string 
    padChar = padChar || ' '; 
    var value = this.toString(); 
    while (value.length < width) { 
     value = padChar + value; 
    } 
    return value; 
}; 
+1

哇,這是非常全面的。 感謝您花時間解釋這一點。我現在不能投票,因爲有人標記我的問題,或者我會對您的答案進行投票。 再次感謝 – praneybehl

相關問題