微信小程时间及将2019/05/20转化为2019-05-20正则
时间: 2023-12-06 15:05:45 浏览: 78
针对您的问题,我可以提供以下两个正则表达式:
1. 匹配微信小程序中的时间格式"yyyy/MM/dd hh:mm:ss",并将其替换为"yyyy-MM-dd hh:mm:ss":
```
(\d{4})/(\d{2})/(\d{2})\s(\d{2}:\d{2}:\d{2})
```
替换为:
```
$1-$2-$3 $4
```
2. 匹配形如"2019/05/20"的日期格式,并将其替换为"2019-05-20":
```
(\d{4})/(\d{2})/(\d{2})
```
替换为:
```
$1-$2-$3
```
使用示例:
```
const wxTime = "2019/05/20 12:30:00";
const replacedWxTime = wxTime.replace(/(\d{4})\/(\d{2})\/(\d{2})\s(\d{2}:\d{2}:\d{2})/, "$1-$2-$3 $4");
console.log(replacedWxTime); // 输出: "2019-05-20 12:30:00"
const dateStr = "2019/05/20";
const replacedDateStr = dateStr.replace(/(\d{4})\/(\d{2})\/(\d{2})/, "$1-$2-$3");
console.log(replacedDateStr); // 输出: "2019-05-20"
```
希望我的回答能够帮到您!
相关问题
微信小程时间及将/转化为-正则
您好,根据您的问题,我可以理解为您想要将"微信小程序时间"和"将/转化为"转化为正则表达式。
针对"微信小程序时间",如果您指的是小程序中的时间格式,一般为"yyyy-MM-dd hh:mm:ss"或"yyyy/MM/dd hh:mm:ss",那么可以使用以下正则表达式进行匹配:
```
\d{4}[-/]\d{2}[-/]\d{2}\s\d{2}:\d{2}:\d{2}
```
针对"将/转化为",如果您想要匹配"将"或"转化为",可以使用以下正则表达式:
```
将|转化为
```
希望我的回答能够帮到您!
微信小程勋 格式化时间
### 微信小程序中的时间格式化方法
在微信小程序中实现时间格式化的两种常见方式如下:
#### 使用 JavaScript 实现 `getRelativeTime` 函数
此函数可以计算给定日期相对于当前时间的描述,适用于消息时间戳、日志记录等场景。对于输入的不同类型的参数(Date对象、数字或字符串),返回类似于 "1小时前" 或者 "昨天" 的描述。
```javascript
export const getRelativeTime = (date) => {
let now = new Date();
let inputDate = new Date(date);
let diffInSeconds = Math.floor((now - inputDate) / 1000);
if(diffInSeconds < 60){
return `${diffInSeconds}秒前`;
}else if(Math.floor(diffInSeconds/60)<60){
return `${Math.floor(diffInSeconds/60)}分钟前`;
}else if(Math.floor(diffInSeconds/(60*60))<24){
return `${Math.floor(diffInSeconds/(60*60))}小时前`;
}else{
var dayDiff = Math.floor(diffInSeconds/(60 * 60 * 24));
switch(dayDiff){
case 1:
return '昨天';
case 2:
return '前天';
default:
if(dayDiff>2 && dayDiff<=7){
return `${dayDiff} 天前`;
}
else{
return inputDate.toLocaleDateString(); // 超过一周则显示具体日期
}
}
}
}
```
上述代码实现了根据不同时间段给出相应的时间描述[^1]。
#### 利用 WXS 进行页面内的时间格式化
另一种方案是在 `.wxml` 文件里利用 `<wxs>` 标签来加载外部定义好的辅助模块来进行时间处理。下面是一个简单的例子展示如何使用这种方法:
```html
<wxs src="./path/to/utils/time.wxs" module="timeFormatter"></wxs>
<view>{{ timeFormatter.formatTime(Date.now()) }}</view>
```
这里假设有一个名为 `formatTime` 的函数存在于 `./path/to/utils/time.wxs` 中,并且它接收 Unix 时间戳作为参数并返回格式化后的字符串表示形式[^2]。
为了更清晰地理解这两种不同的实现途径及其应用场景,在实际项目开发过程中可以根据需求选择合适的方式完成时间数据的呈现工作。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)