简体   繁体   English

更改日期 object 中的日期格式

[英]Changing the format of date in the Date object

I am using Bootstrap 4. I am writing two functions "saveIssue" and "fetchIssue".我正在使用 Bootstrap 4。我正在编写两个函数“saveIssue”和“fetchIssue”。 The saveIssue function takes the details of the form filled through HTML and stores them in localStorage.In the function, it also notes the time of saving the issues with the new Date() object. The saveIssue function takes the details of the form filled through HTML and stores them in localStorage.In the function, it also notes the time of saving the issues with the new Date() object. fetchIssue function takes the details from localStorage and displays them on screen. fetchIssue function 从 localStorage 获取详细信息并将其显示在屏幕上。 While displaying the date of issue obtained through the new Date() object, it is displaying it in the format like 2020-06-02T14:51:21.482Z.在显示通过 new Date() object 获得的发行日期时,它以类似 2020-06-02T14:51:21.482Z 的格式显示。 How can I just display it in a more generalized form and also not int GMT?我怎样才能以更通用的形式显示它而不是 int GMT?

1)fetchIssue function 1)fetchIssue function

function fetchIssues () {
var issues=JSON.parse(localStorage.getItem('issues'));
var I;
for(i=0;i<issues.length;i++)
{
    var id=issues[i].id;
    var description=issues[i].description;
    var severity=issues[i].severity;
    var assignto=issues[i].assignto;
    var status=issues[i].status;
    var deadline=issues[i].deadline;
    var started=issues[i].started;
if(status=="Open")
  {
document.getElementById('issuesList').innerHTML+='<div class="well">'+'<h5> Issue ID : '+ id+'</h5>' 
+'<h4><span class="label label-info">'+ status +'</span></h4>'
+'<h3><span class="glyphicon glyphicon-paste"></span>'+"     "+  description + '</h3>'
+ '<p><span class="glyphicon glyphicon-bell"></span>'+'     '+ severity+'         '
+'<span class="glyphicon glyphicon-user"></span>'+'     '+ assignto + '</p>'+'   '
+'<p><span class="glyphicon glyphicon-calendar"></span>'+'  '+'Issued on :'+' '+started+'</p>'
+'<p><span class="glyphicon glyphicon-time"></span>'+'  '+'Deadline :'+' '+deadline+'</p>' 
+'<button type="button" class="btn btn-success" onclick="Closed(\''+i+'\')">Close</button>'+' '
+'<button type="button" class="btn btn-danger" onclick="Deleted(\''+i+'\')">Delete</button>'+' '
+'<button type="button" class="btn btn-primary" onclick="Reassigned(\''+i+'\')">Reassign</button>';
  }

else
  {
document.getElementById('issuesList').innerHTML+='<div class="well">'+'<h5> Issue ID : '+ id+'</h5>' 
+'<h4><span class="label label-warning">'+ status +'</span></h4>'
+'<h3><span class="glyphicon glyphicon-paste"></span>'+"     "+  description + '</h3>'
+ '<p><span class="glyphicon glyphicon-time"></span>'+'     '+ severity+'         '
+'<span class="glyphicon glyphicon-user"></span>'+'     '+ assignto + '</p>'
+'<button type="button" class="btn btn-info" onclick="Reopened(\''+i+'\')">Reopen</button>'+' '
+'<button type="button" class="btn btn-primary" onclick="Reassigned(\''+i+'\')">Reassign</button>';
  }
}
}

2)saveIssue funtion 2)保存问题功能

function saveIssue(e)
{ 
var id=chance.guid()
var description = document.getElementById("issueDescInput").value;
var severity = document.getElementById("issueSeverityInput").value;
var assignto = document.getElementById("issueAssignedToInput").value;
var status="Open";
var deadline=document.getElementById("deadline").value;
var started=new Date();
var summarized={
    id,
    description,
    severity,
    assignto,
    status,
    deadline,
    started
}
var final=JSON.stringify(summarized);
if(localStorage.getItem('issues')===null)
{
    var issues=[];
    issues.push(summarized);
    var finalissues=JSON.stringify(issues);
    localStorage.setItem('issues', finalissues);
    alert("saved");
}
else
{
    var issues = JSON.parse(localStorage.getItem('issues'));
    issues.push(summarized);
    var finalissues=JSON.stringify(issues);
    localStorage.setItem('issues', finalissues);
    alert("saved");
}
e.preventDefault();
window.location.reload();
}

You can format the date in any way you would like by doing a couple of steps:您可以通过执行几个步骤以任何方式格式化日期:

let currentDate = new Date();
let currentYear = currentDate.getFullYear();
let currentMonth = currentDate.getMonth() + 1 // January is Month 0, so we add one
let currentDay = currentDate.getDay();

These allow you to get the current date (variable currentDate ) and extract different information from it (year, month, day).这些允许您获取当前日期(变量currentDate )并从中提取不同的信息(年、月、日)。 Now you can format it in your way:现在您可以按照自己的方式对其进行格式化:

yyyy-mm-dd:年年-月-日:


    if (currentDay < 10){
        currentDay = '0'+ currentDay // Add a 0 before the day if it is less than 10
    }

    if (currentMonth < 10){
        currentMonth = '0' + currentMonth
    } 

let newDate = currentYear + "-" + currentMonth + "-" + currentDay;

This will display the date in local time.这将显示当地时间的日期。 If you want it in UTC Time:如果你想要它在 UTC 时间:

let currentDate = new Date();
let currentYear = currentDate.getFullUTCYear();
let currentMonth = currentDate.getUTCMonth() + 1 // January is Month 0, so we add one
let currentDay = currentDate.getUTCDay();

And extract:并提取:

yyyy-mm-dd:年年-月-日:

    if (currentDay < 10){
        currentDay = '0'+ currentDay // Add a 0 before the day if it is less than 10
    }

    if (currentMonth < 10){
        currentMonth = '0' + currentMonth
    } 

let newDate = currentYear + "-" + currentMonth + "-" + currentDay;

See it in action:看看它的实际效果:

 function showDate() { let currentDate = new Date(); let currentYear = currentDate.getFullYear(); let currentMonth = currentDate.getMonth() + 1 // January is Month 0, so we add one let currentDay = currentDate.getDay(); if (currentDay < 10){ currentDay = '0'+ currentDay // Add a 0 before the day if it is less than 10 } if (currentMonth < 10){ currentMonth = '0' + currentMonth } let newDate = currentYear + "-" + currentMonth + "-" + currentDay; document.getElementById('seeDate').innerHTML = newDate; }
 <button onclick="showDate()">See the date</button> <div id="seeDate"></div>

You can also you the toLocaleDateString() , toDateString() , toISOString() , or toLocaleString() for easier methods.您也可以使用toLocaleDateString()toDateString()toISOString()toLocaleString()以获得更简单的方法。 But I would stick to the ones above because are able to easily change the format in seconds.但我会坚持上面的那些,因为能够在几秒钟内轻松更改格式。

I hope this helps!我希望这有帮助!

You can also use these methods.您也可以使用这些方法。

var started = new Date();

Date.toLocaleDateString() Date.toLocaleDateString()

console.log(started.toLocaleDateString()); // "02/06/2020"

Date.toDateString() 日期.toDateString()

console.log(started.toDateString()); // "Tue Jun 02 2020"

Date.toISOString() 日期.toISOString()

console.log(started.toISOString()); // "2020-06-02T15:27:29.780Z"

Date.toLocaleString() Date.toLocaleString()

console.log(started.toLocaleString()); // "02/06/2020, 23:27:29"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM