[英]javascript dates with same date format
I am facing an issue in javascript dates.我在 javascript 日期中遇到问题。 i want to do same date format both previous date
and start date
我想对previous date
和start date
做相同的日期格式
Excepted Output
previous date Excepted Output
上一日期
after 12AM shows 00:00:00 to 12:00:00 12AM 之后显示 00:00:00 到 12:00:00
after 12PM shows 00:00:00 to 12:00:00 12PM 后显示 00:00:00 至 12:00:00
format格式
previous date 2020-05-10 16:31:28
start date开始日期
2020-05-10 02:00:00 //sample data
2020-05-10 05:00:00
My code:我的代码:
var currentdate = new Date();
var prevdate = new Date(); //previous date
prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));
var newprevious = GetFormattedDate(prevdate);
console.log(newprevious);
//Date format yyyy-mm-dd h:MM:ss
function GetFormattedDate(date) {
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + (date.getDate())).slice(-2);
var year = date.getFullYear();
var hour = ("0" + (date.getHours())).slice(-2);
var min = ("0" + (date.getMinutes())).slice(-2);
var seg = ("0" + (date.getSeconds())).slice(-2);
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg;
}
Note: i can't change the start_date it is fixed
format注意:我无法更改 start_date 它是fixed
格式
what should i change?我应该改变什么? Anyone help me?有人帮我吗?
If the problem is just that the hour is not in am/pm format, you can modify your function this way, using the modulo operator (I've also added am/pm at the end of the string):如果问题只是小时不是 am/pm 格式,您可以使用模运算符以这种方式修改 function(我还在字符串末尾添加了 am/pm):
var currentdate = new Date(); var prevdate = new Date(); //previous date prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000)); var newprevious = GetFormattedDate(prevdate); console.log(newprevious); //Date format yyyy-mm-dd h:MM:ss function GetFormattedDate(date) { var month = ("0" + (date.getMonth() + 1)).slice(-2); var day = ("0" + (date.getDate())).slice(-2); var year = date.getFullYear(); // EDIT: var hour = date.getHours()%12; // using the modulo operator, you convert the hours to a range between 0 and 12 if (hour == 0) { hour = 12; } var timeOfDay = "am"; if (date.getHours() >= 12) { timeOfDay = "pm"; } hour = ("0" + hour).slice(-2); // END EDIT var min = ("0" + (date.getMinutes())).slice(-2); var seg = ("0" + (date.getSeconds())).slice(-2); return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg + " " + timeOfDay; }
This really is a date formatting issue so probably a duplicate of How to format a JavaScript date .这确实是一个日期格式问题,因此可能与如何格式化 JavaScript 日期重复。
As Drago96 has answered, you can use the remainder operator % to modify the hours value and add am or pm as appropriate正如 Drago96 已经回答的那样,您可以使用余数运算符 % 来修改小时值并根据需要添加 am 或 pm
An alternative to using get methods and processing each part is to use the formatting options of Intl.DateTimeFormat object and formatToParts , eg使用get方法和处理每个部分的替代方法是使用Intl.DateTimeFormat object和formatToParts的格式化选项,例如
function formatDate(date) { let parts = new Intl.DateTimeFormat('default',{ year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true }).formatToParts(date).reduce((acc, part) => { if (part.type.= 'literal') { acc[part.type] = part;value; } return acc, }. Object;create(null)). return `${parts.year}-${parts.month}-${parts.day}\ ${parts:hour}.${parts:minute}.${parts.minute}\ ${parts;dayPeriod}`. } console;log(formatDate(new Date()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.