js 将long日期格式 转换为标准日期格式方法
我们经常在操作的时候会发现从后台传递到view层的json中datetime类型变成了long型,当然你也可以从后台先转为string类型,但是如果是从和数据库对应的object中封装的话,就不能再去转为string类型了,所以需要在js中进行序列化为我们常见的标准日期格式。
直接上代码,希望可以帮助到大家(另:本人近几天开始做一个 MVC4.0+WCF+EF+Bootstrap的架构系列博文,希望大家支持和指正):
<script language="javascript">
//扩展Date的format方法
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
/**
*转换日期对象为日期字符串
* @param date 日期对象
* @param isFull 是否为完整的日期数据,
* 为true时, 格式如"2013-12-06 01:05:04"
* 为false时, 格式如 "2013-12-06"
* @return 符合要求的日期字符串
*/
function getSmpFormatDate(date, isFull) {
var pattern = "";
if (isFull == true || isFull == undefined) {
pattern = "yyyy-MM-dd hh:mm:ss";
} else {
pattern = "yyyy-MM-dd";
}
return getFormatDate(date, pattern);
}
/**
*转换当前日期对象为日期字符串
* @param date 日期对象
* @param isFull 是否为完整的日期数据,
* 为true时, 格式如"2013-12-06 01:05:04"
* 为false时, 格式如 "2013-12-06"
* @return 符合要求的日期字符串
*/
function getSmpFormatNowDate(isFull) {
return getSmpFormatDate(new Date(), isFull);
}
/**
*转换long值为日期字符串
* @param l long值
* @param isFull 是否为完整的日期数据,
* 为true时, 格式如"2013-12-06 01:05:04"
* 为false时, 格式如 "2013-12-06"
* @return 符合要求的日期字符串
*/
function getSmpFormatDateByLong(l, isFull) {
return getSmpFormatDate(new Date(l), isFull);
}
/**
*转换long值为日期字符串
* @param l long值
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
* @return 符合要求的日期字符串
*/
function getFormatDateByLong(l, pattern) {
return getFormatDate(new Date(l), pattern);
}
/**
*转换日期对象为日期字符串
* @param l long值
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
* @return 符合要求的日期字符串
*/
function getFormatDate(date, pattern) {
if (date == undefined) {
date = new Date();
}
if (pattern == undefined) {
pattern = "yyyy-MM-dd hh:mm:ss";
}
return date.format(pattern);
}
//alert(getSmpFormatDate(new Date(1279849429000), true));
//alert(getSmpFormatDate(new Date(1279849429000),false));
//alert(getSmpFormatDateByLong(1279829423000, true));
alert(getSmpFormatDateByLong(1279829423000,false));
//alert(getFormatDateByLong(1279829423000, "yyyy-MM"));
//alert(getFormatDate(new Date(1279829423000), "yy-MM"));
//alert(getFormatDateByLong(1279849429000, "yyyy-MM hh:mm"));
</script>
js 将long日期格式 转换为标准日期格式方法的更多相关文章
- js将long日期格式转换为标准日期格式
<script language="javascript"> //扩展Date的format方法 Date.prototype.format = function (f ...
- 转换为标准IPv4格式
Insus.NET刚写了一个函数,把一个IP地址转换为标准格式,即每段位均是由3个数字组成. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- = ...
- C# DateTime时间格式转换为Unix时间戳格式
double ntime=dateTimeToUnixTimestamp(DateTime.Now); long g1 = GetUnixTimestamp(); long g2 = ConvertD ...
- DateTime时间格式转换为Unix时间戳格式
/// <summary> /// 将DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="date ...
- SQLServer 将日期改造成标准日期格式(如: 2016/6 ->201606)
同事给了份Excel 数据,导到数据库之后再查出来时发现顺序不好弄.于是想从数据源中做处理. 由于数据存在,年/月 与 年/月/日 的格式不好用datetime保存,于是用varchar保存. 数据处 ...
- vue框架中的日期组件转换为yyy-mm-dd格式
最近在用vue框架写一个app,这个是用到的日期格式转换,把下面的标准格式转换为字符串连接格式
- [工具类]将时间转换为unix时间戳格式
写在前面 由于在数据库中存的时间有时间戳格式的数据,在解析以及保存的时候,就需要考虑到数据格式的兼容性问题.看到数据库中的时间字段基本上都是以时间戳格式存储的,没办法,只能将时间进行转换了,考虑到其他 ...
- C#时间格式转换为时间戳互转
/// <summary> /// 将 DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="tim ...
- c# DateTime时间格式和JAVA时间戳格式相互转换
/// java时间戳格式时间戳转为C#格式时间 public static DateTime GetTime(long timeStamp) { DateTime dtStart = TimeZon ...
随机推荐
- Poj Roadblocks(次短路)
Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...
- Java设计模式(四) 装饰 代理模式
(七)装饰 Decorator 装饰是一个对象,以动态地增加一些新功能. 象与被装饰的对象须要实现同一个接口.装饰对象持有被装饰对象的实例. interface DecoratorSourceable ...
- Linux内核和根文件系统引导加载程序
续博文<u-boot之u-boot-2009.11启动过程分析> Linux内核启动及文件系统载入过程 当u-boot開始运行bootcmd命令.就进入Linux内核启动阶段,与u-boo ...
- 关于启动调试时,总是启动多个web端口的问题
在一个解决方案中,经常包含多个web项目,当设置某一个web项目为起始项目后,启动调试,会发现其他的web项目的端口也会一起打开,显示在电脑的右下方,其实开启其他端口有时是没必要的,只是多占用了系统内 ...
- error:stray'\243'in program
error:stray'\243'in program 问题: C原因在编译时出现例如以下错误 error:stray'\243'in program 错误原因: 非 - 标准ascII字 ...
- MVC 接受Flash上传图片
/// <summary> /// 经Flash上传图片 /// </summary> /// <param name="uid"></p ...
- cocos2d-x 移植android竖,横屏设置
AndroidManifest.xml于android:screenOrientation现场控制屏幕方向,默认为横屏 android:screenOrientation="landscap ...
- 关于fork()函数的作用
(1) 先看一个实例: #include <unistd.h>; #include <sys/types.h>; main () { pid_t pi ...
- 【solr这四个主题】在Tomcat 部署Solr4.x
1.安装Tomcat (1)下载并解压缩到/opt/tomcat在 # cd /opt/jediael # tar -zxvf apache-tomcat-7.0.54.tar.gz # mv apa ...
- sql注入工具sqlmap使用参数说明
Options(选项):--version 显示程序的版本号并退出-h, --help 显示此帮助消息并退出-v VERBOSE 详细级别:0-6(默认为1)Target(目标):以下至少需要设置其中 ...