简体   繁体   中英

how to convert date string to normal readable date

i have date string which looks like this 2017-11-09T05:36:00.834Z

I want this date string to be in readable format like this DD/MM/YYYY

I tried something like this but giving error .

var formattedDate = new Date('2017-11-09T05:36:00.834Z').format('DD/MM/YYYY');

please help me to get the formated date

thanks in advance!!!!!

您可以使用moment.js来做到这一点。

var date = moment('2017-11-09T05:36:00.834Z').format('DD/MM/YYYY');

Try

substring -> split -> reverse -> join

 var input = "2017-11-09T05:36:00.834Z" var output = input.substring(0,10).split("-").reverse().join( "/" ); console.log( output ); 

var date= new Date(`2017-11-09T05:36:00.834Z`);
var dd = date.getDate();
var mm = date.getMonth()+1; //January is 0!

var yyyy = date.getFullYear();
if(dd<10){
    dd='0'+dd;
} 
if(mm<10){
    mm='0'+mm;
} 
var date= dd+'/'+mm+'/'+yyyy;
console.log(date);//it logs "09/11/2017"
   Use this Function 

   function dateFor(value) {  
            if(value==null) {
                return '-' 
            } else {    
                var date = new Date(value); 
                var dates = date.toDateString();
                var datess = dates.split(' ');     
                if(date.getMonth()+1>=10) {
                    return datess[2]+'-'+(date.getMonth()+1)+'-'+datess[3];
                } else {      
                    return datess[2]+'-'+'0'+(date.getMonth()+1)+'-'+datess[3];
                }
            }    
        }; 
var dateString = '2017-11-09T05:36:00.834Z'
var date = new Date(dateString);
var day = date.getDate();
var year = date.getFullYear();
var month = date.getMonth()+1;
console.log( day + '/' + month + '/' + year);

You can use Jquery dateFormat Js Plugin.

<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-dateFormat/1.0/jquery.dateFormat.min.js"></script>
        <script>
        jQuery(document).ready(function() {
            var shortDateFormat = 'dd/MM/yyyy';
            var date1 = "2017-11-09T05:36:00.834Z";
            var date2 = jQuery.format.toBrowserTimeZone(date1,shortDateFormat);
            alert(date2);
        });
        </script>
    </body>
</html>

A useful and flexible way for formatting the DateTimes in javascript is:

var date = new Date('2017-11-09T05:36:00.834Z');
// optional: hour: '2-digit', minute: '2-digit', timeZoneName: 'short'
var options = { year: 'numeric', month: '2-digit', day: '2-digit'};
console.log(new Intl.DateTimeFormat('en-GB', options).format(date));

Result: "09/11/2017"

In this way, you can customize all dates.

an alternative way is(without options):

var formattedDate=new Date('2017-11-09T05:36:00.834Z').toLocaleDateString('en-GB');
console.log(formattedDate);

Result: "09/11/2017"

Good Luck

Check Online

If you're after a way that simply works on string manipulation, then you could use a regular expression to take the relevant parts out:

 var formattedDate = "2017-11-09T05:36:00.834Z" var result = formattedDate.replace(/^(\\d{4})-(\\d{2})-(\\d{2}).*$/, "$3/$2/$1"); console.log (result); 

The first parameter captures a four-digit number and two two-digit number, while the second parameter includes the relevant placeholders.

However, you're dealing with a date, so by far the best way would be to use the date formatting functions mentioned in the answers to Where can I find documentation on formatting a date in JavaScript? .

Hope this can help you.

let d = new Date('2017-11-09T05:36:00.834Z');
var formatted = `${d.getDate()}/${d.getMonth() + 1}/${d.getFullYear()}`;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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