js时间戳格式化

本文介绍了一种格式化时间戳的JavaScript方法,该方法能够根据不同输入格式的时间戳(包括包含CST和GMT的时间字符串)转换为统一的日期格式。此函数通过解析时间戳,提取年月日等信息,并按指定格式输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//格式化时间戳
function formatTimestamp(dateTime,isShowTime){

if(dateTime==null || dateTime==""){
return "";
}
var date = new Date(dateTime);
var time = "";
var year = "";
var day = "";
var month = date.getMonth()+1;
month = month<10?("0"+month):month;
//Mon May 21 15:04:36 CST 2018
if(dateTime.toString().indexOf("CST")!=-1){
var arr = dateTime.split(" ");
year = arr[5];
time= arr[3];
day = arr[2];
}else if(date.toString().indexOf("GMT")!=-1){//Sun May 20 2018 10:12:13 GMT+0800 (中国标准时间)
var arr = date.toString().split(" ");
    year = arr[3];
time= arr[4];
day = arr[2];
}
var reDate = year+"-"+month+"-"+day;
    //显示时间
    if(isShowTime){
    reDate += " " +time;
    }
    
    return reDate;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值