Use JS to convert Long data to date type or datetime type __JS

Source: Internet
Author: User
Tags getdate

The date format stored in the database is dated (YYYY-MM-DD format date) and datetime (YYYY-MM-DD 00:00:00 format date), when the date data in the database is passed to the front end with a Java program, if the background does not have a method to process the date, The long data is passed to the front end. For example:
Database stored on date: 2017-01-06 20:30:00
The date format for the front-end acquisition (interacting via Ajax) is: 1483705800000
The correct date format should be displayed on the front-end page.
The following is a JS method that converts a long to date or DateTime format:

return YYYY-MM-DD format Date (i):

function Dateformat_1 (longtypedate) {  
    var datetype = "";  
    var date = new Date ();  
    Date.settime (longtypedate);  
    Datetype + + date.getfullyear ();   Year  
    Datetype = "-" + getmonth (date);//month   
    Datetype + = "-" + getday (date);   Daily return  
    datetype;
} 
Returns the month value   
function getmonth (date) {  
    var month = "" of 01-12;  
    month = Date.getmonth () + 1; GetMonth () The resulting month is 0-11  
    if (month<10) {  
        month = "0" + month;  
    }  
    Return month;  
}  
Returns a date of 01-30  
function getday (date) {  
    var day = "";  
    Day = Date.getdate ();  
    if (day<10) {Day  
        = ' 0 ' + day;  
    }  
    return day;  
}

return YYYY-MM-DD format date (ii):

function Dateformat_2 (longtypedate) {  
    var datetype = "";  
    var date = new Date ();  
    Date.settime (longtypedate); 
    Datetype = date.getfullyear () + "-" +getmonth (date) + "-" +getday (date);//yyyy-mm-dd format date return
    Datetype;

return YYYY-MM-DD 00:00:00 format date (i)

function Datetimeformat_1 (longtypedate) {var datetimetype = "";  
    var date = new Date ();  
    Date.settime (longtypedate);   datetimetype+= date.getfullyear (); Year datetimetype+= "-" + getmonth (date);   Month Datetimetype + = "-" + getday (date);   Day datetimetype+= "&nbsp;&nbsp;" + getHours (date);      When datetimetype+= ":" + getminutes (date);      Minute datetimetype+= ":" + getseconds (date);
Separate return datetimetype;  
    //Returns the month Value function getmonth (date) {var month = "" of 01-12; month = Date.getmonth () + 1;  
    The month getmonth () is 0-11 if (month<10) {month = "0" + month;  
Return month;  
    //Returns a date of 01-30 function getday (date) {var day = "";  
    Day = Date.getdate ();  
    if (day<10) {day = ' 0 ' + day;  
Return day;
    //Returns the Hour function getHours (date) {var hours = "";
    Hours = Date.gethours ();  
    if (hours<10) {hours = "0" + hours;  
   } return hours;
    //Returns the function getminutes (date) {var minute = "";
    minute = Date.getminutes ();  
    if (minute<10) {minute = "0" + minute;  
return minute;
    //Returns the second function getseconds (date) {var second = "";
    Second = Date.getseconds ();  
    if (second<10) {second = "0" + second;  
return second; }

return YYYY-MM-DD 00:00:00 format date (ii)

function Datetimeformat_2 (longtypedate) {  
    var datetimetype = "";  
    var date = new Date ();  
    Date.settime (longtypedate);
    Datetimetype = date.getfullyear () + "-" +getmonth (date) + "-" +getday (date) + "&nbsp;" +gethours (date) + ":" +getminutes (date) + ":" +getseconds (date);//yyyy-mm-dd 00:00:00 format date return
    Datetimetype;

Call Method:
Datetimeformat_1 (1483705800000);
The results obtained are: 2017-01-06 20:30:00

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.