Created
September 9, 2014 00:03
-
-
Save thomasjao/6acf2aacd92e6080fa7a to your computer and use it in GitHub Desktop.
JSON 的資料型態不包含 date,往往需另行處理日期相關的運算,頗為麻煩。若能將日期儲存為數值,則於運算時可便利很多。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var birthday = '1962/7/25'; | |
function date2JSON( dt ) { | |
var dayArr = dt.split('/'); | |
return JSON.parse( '{ "year":' + parseInt( dt[0] ) + ', "month:"' + parseInt( dt[1] ) + ', "day":' + parseInt( dt[3] ) + '}'); | |
Usage Example: | |
var myBirthday = date2JSON( birthday ); | |
==> {"year": 1962, "month": 7, "day": 25} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment