JavaScript时间戳与其格式化

在 PHP + MySQL (日期类型为datetime) + ajax 应用中,有时候需要用 JavaScript 将时间戳类型格式化为一般的时间类型格式。下面提供一些转换的方法,比较常见的一些总结。

先定义时间戳与其Date格式日期

var day1 = parseInt(new Date().valueOf()/1000);
var day2 = new Date(day1 * 1000);

下面是从时间戳获得日期的封装方法,与day2方式差不多:

function getLocalTime(nS) {  
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');  
} 

replace 一下:

function getLocalFormatTime(nS) {  
	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");   
}

一个比较笨的获得格式化日期的方法:

document.getElementById("btn5").onclick = function(){
	alert(day2.getFullYear()+"-"+(day2.getMonth()+1)+"-"+day2.getDate()+" "+day2.getHours()+":"+day2.getMinutes()+":"+day2.getSeconds());
}

下面是完整程序:

<script type="text/javascript">
var day1 = parseInt(new Date().valueOf()/1000);
var day2 = new Date(day1 * 1000);

function getLocalTime(nS) {  
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');  
} 

/* 同上面函数 */
function getLocalTimes(nS) {  
    return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17);
}  

function getLocalFormatTime(nS) {  
	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");   
}
    
document.getElementById("btn1").onclick = function(){
	alert(day1);
}

document.getElementById("btn2").onclick = function(){
	alert(day2.toLocaleString());
}

document.getElementById("btn3").onclick = function(){
	alert( getLocalTime(day1) );
}

document.getElementById("btn4").onclick = function(){
	alert( getLocalFormatTime(day1) );
}

document.getElementById("btn5").onclick = function(){
	alert(day2.getFullYear()+"-"+(day2.getMonth()+1)+"-"+day2.getDate()+" "+day2.getHours()+":"+day2.getMinutes()+":"+day2.getSeconds());
}
</script>

本文地址:http://www.nowamagic.net/librarys/veda/detail/399,欢迎访问原出处。

Linux系统中C语言处理时间戳格式化的常见方式主要是通过`time.h`头文件中提供的函数。下面是几个常用的操作: 1. 获取当前时间的时间戳(秒级): 使用`time()`函数可以获得从Epoch(1970年1月1日 00:00:00 UTC)开始计算的当前时间的秒数,返回一个`time_t`类型的值。 ```c #include <time.h> #include <stdio.h> int main() { time_t current_time = time(NULL); printf("当前时间的时间戳是:%ld\n", current_time); return 0; } ``` 2. 将时间戳转换为可读的字符串格式: 使用`ctime()`函数可以将时间戳转换为本地时间的字符串表示,包括日期和时间。 ```c #include <time.h> #include <stdio.h> int main() { time_t current_time = time(NULL); printf("当前时间的可读格式是:%s", ctime(&current_time)); return 0; } ``` 3. 格式化时间输出: 使用`strftime()`函数可以将`struct tm`类型的对象转换成特定格式的时间字符串。首先需要使用`localtime()`或`gmtime()`函数将时间戳转换为`struct tm`结构体,然后进行格式化。 ```c #include <time.h> #include <stdio.h> int main() { time_t current_time = time(NULL); struct tm *time_info = localtime(&current_time); char buffer[80]; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", time_info); printf("当前时间的格式化输出是:%s\n", buffer); return 0; } ``` 4. 字符串解析为时间: 使用`strptime()`函数可以从字符串中解析出时间,并填充到`struct tm`结构体中。 ```c #include <time.h> #include <stdio.h> int main() { struct tm time_info = {0}; char time_str[] = "2023-03-14 12:34:56"; strptime(time_str, "%Y-%m-%d %H:%M:%S", &time_info); // 可以继续使用strftime等函数进行格式化输出或其他操作 char buffer[80]; strftime(buffer, sizeof(buffer), "%c", &time_info); printf("解析的时间格式化输出是:%s\n", buffer); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值