javascript - 将 mysql 表中的日期字符串转换为特定格式
问题描述
在下面的示例中,我需要20-Jan-2021
在里面inpdate
将一个字符串 -2021-01-20
从服务器端给出 - 转换为相应的格式
有什么帮助吗?
let a = "2021-01-20"; // given from mysql table
let b = ???;
$('#inpdate').val(b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' id='inpdate'>
解决方案
您可以从此处给出的日期创建一个新的 Date 对象,然后访问其属性以构建您喜欢的字符串:
function reformatDate(dateString) {
const dateObject = new Date(dateString);
return `${dateObject.getDate()}-${dateObject.toLocaleString('default', { month: 'short' })}-${dateObject.getFullYear()}`;
}
console.log(reformatDate('1970-01-01'));
console.log(reformatDate('2021-01-20'));
推荐阅读
- node.js - Synology SSO 的 Passport.js 策略
- android - 应用程序在 tabhost 中的 setContent(new Intent) 上崩溃
- google-analytics-api - 通过 API 在 Google Analytics 中读取用户权限 - 不适用于属性级别
- javascript - 使用 .on('click') 添加的 CSS 类在第一个事件后不可见
- tensorflow - tensorflow/keras 模型的内存不足 (OOM) 错误
- javascript - 隐藏多个ID的溢出
- sql - Excel VBA宏中的SQL查询不显示聚合函数?
- sumo - 为什么通过属性的流元素在 SUMO 路由文件中不起作用
- openpyxl - 如何计算行数,openpyxl
- machine-learning - 处理不平衡数据的样本权重与下采样