Skip to content

Instantly share code, notes, and snippets.

@thomasjao
Created September 9, 2014 00:03
Show Gist options
  • Save thomasjao/6acf2aacd92e6080fa7a to your computer and use it in GitHub Desktop.
Save thomasjao/6acf2aacd92e6080fa7a to your computer and use it in GitHub Desktop.
JSON 的資料型態不包含 date,往往需另行處理日期相關的運算,頗為麻煩。若能將日期儲存為數值,則於運算時可便利很多。
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