user5405873
user5405873

Reputation:

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!!!!!

Upvotes: 0

Views: 1730

Answers (9)

Iman Bahrampour
Iman Bahrampour

Reputation: 6800

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

Upvotes: 2

Adrian Wragg
Adrian Wragg

Reputation: 7401

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?.

Upvotes: 1

vijay rami
vijay rami

Reputation: 535

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>

Upvotes: 1

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

You could use moment.js to do it.

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

Upvotes: 2

Sachchit Bansal
Sachchit Bansal

Reputation: 496

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);

Upvotes: 1

Raja Ramachandran
Raja Ramachandran

Reputation: 456

   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];
                }
            }    
        }; 

Upvotes: 1

Rohit Agrawal
Rohit Agrawal

Reputation: 1521

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"

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68363

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 );

Upvotes: 1

Tavish Aggarwal
Tavish Aggarwal

Reputation: 1060

Hope this can help you.

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

Upvotes: 0

Related Questions