MySQL的datetime类型在web端生成javascript的Date对象
2012-04-03 18:00
525 查看
服务端读取Comment表并将结果集返回给调用者,调用者使用json.dumps将结果集转成json字符串列表返回给WEB前端。datetime字段dumps之后变成描述日期的字符串,例如'2012-4-1 12:31:01' JS无法操作这个日期,例如在该时间的基础上加减N天或者N个小时,再比如对两个时间进行比较。找出较早的时间,所以服务端先把datetime转成bigint,再传给客户端(如果是日期是MYSQL的timestamp类型就不需要转换,因为timestamp就是bigint)
JS的Date对象构造函数有多个重载版本,有一个版本可以根据int类型描述的时间生成Date对象(自1970年以来的微秒数,0就是1970-1-1 08:00:00 0000),不过datetime存的是1970以后的秒数,因此初始化时还需要*1000。
服务端使用的时间是标准时区,客户端还需要根据本地时区对服务端的时间做矫正,例如北京时间的10:00提交了一个评论到服务器,这个时间是标准时间的02:00,所以服务器存的也是02:00,从服务器直接读回来就不是用户提交时的本地时间,用户就会觉得奇怪,因此WEB端使用JS去读取用户时区,根据用户所在时区和标准时区的差来矫正结果,例如北京时间就需要在数据库时间上+8个小时,还原到用户提交时的本地时间。
#webpy框架 import web,time,datetime def getList(self,db,noteId): try: sql = "SELECT * FROM Comment where noteId=$noteId" rs = db.query(sql,vars=locals()).list() for r in rs: if r.postTime == None: r.postTime = 0 continue r.postTime = int(time.mktime(r.postTime.timetuple())) d = time.localtime(r.postTime) s = time.strftime('%Y-%m-%d %H:%M:%S', d) return rs except: raise
JS的Date对象构造函数有多个重载版本,有一个版本可以根据int类型描述的时间生成Date对象(自1970年以来的微秒数,0就是1970-1-1 08:00:00 0000),不过datetime存的是1970以后的秒数,因此初始化时还需要*1000。
服务端使用的时间是标准时区,客户端还需要根据本地时区对服务端的时间做矫正,例如北京时间的10:00提交了一个评论到服务器,这个时间是标准时间的02:00,所以服务器存的也是02:00,从服务器直接读回来就不是用户提交时的本地时间,用户就会觉得奇怪,因此WEB端使用JS去读取用户时区,根据用户所在时区和标准时区的差来矫正结果,例如北京时间就需要在数据库时间上+8个小时,还原到用户提交时的本地时间。
/* JavaScript允许通过prototype扩展标准库对象的方法,该函数就是给Date对象增加一个format方法,该方法根据用户的参数描述的时间格式返回一个字符串 var d = new Date(); s=d.format("yyyy-MM-dd HH:mm:ss"); alert(s); */ Date.prototype.format=function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时 "H+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth()+3)/3), //季度 "S" : this.getMilliseconds() //毫秒 }; var week = { "0" : "\u65e5", "1" : "\u4e00", "2" : "\u4e8c", "3" : "\u4e09", "4" : "\u56db", "5" : "\u4e94", "6" : "\u516d" }; if(/(y+)/.test(fmt)){ fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } if(/(E+)/.test(fmt)){ fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getDay()+""]); } 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))); } } return fmt; } function convertUTCTolocalTime(serverTime) { var d = new Date(); //getTimezoneOffset()可以取得JS运行环境的时区和标准时区的分钟差 var localZone = d.getTimezoneOffset(); serverTime = parseInt(serverTime) if(serverTime == 0) return "" if(localZone == 0) { //服务器存的时间是秒需要*1000变成微秒 return new Date(parseInt(serverTime)*1000).format("yyyy-MM-dd HH:mm:ss") } if(localZone < 0) { localZone = Math.abs(localZone); localTime = new Date(parseInt(serverTime)*1000).valueOf(); //标准时间 + (1000 * 60 是1分钟 * 偏差分钟数) == JS环境的本地时间 localTime = localTime + (1000 * 60 * localZone); return new Date(localTime).format("yyyy-MM-dd HH:mm:ss") } if(localZone > 0) { localZone = Math.abs(localZone); localTime = new Date(parseInt(serverTime)*1000).valueOf(); //标准时间 - (1000 * 60 是1分钟 * 偏差分钟数) == JS环境的本地时间 localTime = localTime - (1000 * 60 * localZone); return new Date(localTime).format("yyyy-MM-dd HH:mm:ss") } return "tntime.js: convertUTCTolocalTime has error" }
相关文章推荐
- Java Date类型和mysql中 Datetime类型的转换
- mysql中date与datetime类型的区别
- 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间
- C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题
- 详解JavaScript对Date对象的操作问题(生成一个倒数7天的数组)
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别
- MySQL5.0的三种数据类型:DATETIME, DATE和TIMESTAMP的使用
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别
- mysql所支持的日期时间类型有:DATETIME、 TIMESTAMP、DATE、TIME、YEAR。
- 【Web前端学习笔记】Javascript_03_常用对象:String,Number,Math,Date,网页时针
- Java Date和MySQL datetime数据类型转换
- ASP.NET Ajax, JQuery & JSON Date Serialization,.net对象系列化为JSON的时候,DateTime类型的转换问题
- Mysql 之Date,DateTime,TimeStamp三种类型的详解
- MySQL数据类型 DATETIME和 DATE
- MySQL date、datetime和timestamp类型的区别
- mysql中DATETIME、DATE 和 TIMESTAMP 类型数据使用
- double类型日期转换为Javascript Date对象
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别