json查询日期后 前台转换显示
对于在数据库存储的日期,如果我们想返回到前台显示的话 我们可以使用
spring mvc的@responseBody注解,然后可以通过jackson 在实体类通过@jsonFormat 定义日期的格式返回到前台中,但是这一次 修改的话 挺麻烦,所以就百度json日期格式转换日期正常格式,抱着试一试的样子 找到了 记录一下 也做以后的复习
需要注意的是:下面对jsonDate进行了一个replace的处理,这要看你传过来的日期是什么格式
如果你的格式本身就是 date: 1231312318 这种 就不需要处理了
需要处理的是传递过来如果是这种 /Date()/23131 日期这种格式的
- function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式
- try {
- var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
- var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
- var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
- var hours = date.getHours();
- var minutes = date.getMinutes();
- var seconds = date.getSeconds();
- var milliseconds = date.getMilliseconds();
- return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;
- } catch (ex) {
- return "";
- }
- }
将查询的日期数据 传递到上面jsonDate里 进行下面的转换
然后表格种 就可以正常使用了
贴几个当初搜索时 的链接地址
https://www.cnblogs.com/kissdodog/p/5419923.html
- function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式
- try {
- var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
- var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
- var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
- var hours = date.getHours();
- var minutes = date.getMinutes();
- var seconds = date.getSeconds();
- var milliseconds = date.getMilliseconds();
- return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;
- } catch (ex) {
- return "";
- }
- }

浙公网安备 33010602011771号