最近有一个需求,需要在DoraCMS的系统当中加入一个日期和选择控件,并且要求与Bmob后台数据库的日期格式匹配。
首先我们需要了解Bmob的date类型,它与普通的date不同,是这样的格式:
| 
					 1 2 3 4 5 6 7  | 
						if (_.isDate(value)) {     return {         "__type": "Date",         "iso": value.toJSON()     }; }  | 
					
它是一个json类型,第一个键值对是type和Date,第二个键值对是具体时间,是String类型。
所以我们首先加上Date控件,具体可以参考这个链接。
我的实际代码如下
| 
					 1 2 3 4 5 6 7  | 
						<div class="form-group">     <label class="control-label col-sm-4">过期时间</label>     <div class="col-sm-6">         <input type="datetime-local" class="form-control" name="expiredate" ng-model="formData.expiredate"/>     </div> </div>  | 
					
其中input的类型是datetime-local,是一个单独的时间类型,但是我们知道,我们从后台获取的expire值是bmob数据库的类型,是上面提到的json形式,然而在这个地方我们只需要时间与控件中的iso的内容对应即可,所以ng-model不能为formData.expire.在这里我的处理方式是这样的,我在controller中定义了一个新的变量叫做formData.expiredate,它默认为expire.iso的值,并转化为时间格式,代码如下
| 
					 1 2 3 4 5 6 7 8 9 10 11 12  | 
						if(editId){     getItemService.itemInfo(pageData.bigCategory,editId).success(function(result){         $scope.formData = result;         $scope.targetID = editId;         $scope.formData.expiredate = new Date(result.expire.iso);         initTreeDataByType($scope,$http,"contentCategories");         initContentTags($scope,$http);     }) }else{     $scope.formData = {}; }  | 
					
这样在页面上相当于将输入时间与expiredate这个变量双向绑定,可以正常显示expiredate的值了,在数据库交互是只要把expiredate的值写入expire中即可。但是因为上面这一步生成的格式是这样的Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)
我们直接把这个写入iso是不行的,所以需要对JS的date类型进行转码,转码主要参考该链接的函数。
实际使用时,因为Date的函数原型中并没有这个,所以我写到了函数里,直接对Date类型进行处理。
具体如下:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  | 
						if(req.body.expiredate){     var fmt = "yyyy-MM-dd hh:mm:ss"     var time = new Date(req.body.expiredate);     var o = {         "M+": time.getMonth() + 1, //月份         "d+": time.getDate(), //日         "h+": time.getHours(), //小时         "m+": time.getMinutes(), //分         "s+": time.getSeconds(), //秒         "q+": Math.floor((time.getMonth() + 3) / 3), //季度         "S": time.getMilliseconds() //毫秒     };     if (/(y+)/.test(fmt)){         fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));     }     for (var k in o){         if (new RegExp("(" + k + ")").test(fmt)){             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));             }     }     req.body.expiredate = fmt; }else{     req.body.expiredate = "2020-01-01 00:00:00" }  | 
					
要注意其中的这条语句:var time = new Date(req.body.expiredate);
虽然传回来的已经是date类型了,但是还是要加这条才不会出错,不然上面的.getMonth这些函数都无法使用。
最后写入到expire中,代码如下:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | 
						 updatemessage = {     "occupied" : Boolean(req.body.occupied),     "username" : req.body.username,     "profession" : req.body.profession,     "studentid" : req.body.studentid,     "tags" : req.body.tags,     "category" : req.body.category,     "status" : req.body.status,     "school" : req.body.school,     "gender" : req.body.gender,     "faculty" : req.body.categoryname,     "expire":{         "__type": "Date",         "iso": req.body.expiredate     }, };  | 
					
大功告成~
