1

could you please tell me how to convert date one formate to another in javascript ?

var date = new Date("24 May 2017, 05:35");
d=date.getDate();
m=date.getMonth();
y=date.getYear();
h=date.getHours();
m=date.getMinutes();
console.log(m +' '+d +' '+y+', ' + h+m)


// expected output
//May 24, 2017, 05.35 AM IST

https://jsfiddle.net/bbbnxfz8/

I don't want to use any library like moment

user5711656
  • 2,157
  • 3
  • 23
  • 46

3 Answers3

0

For example if you have

var date = new Date("24 May 2017, 05:35");
var day1 = date.getDate();

You can convert like this -->

var date2 = new Date(date);
var day = date2.getDay();

You can pass a object always content a date form

Do you like this?

0
// Creating Date object
var date = new Date("24 May 2017, 05:35");

// fetching date
d  =  date.getDate();

// fetching month
m  =  date.getMonth();

// fetching year
y  =  date.getFullYear();

// fetching hours
h  =  date.getHours();

// fetching minutes
min=  date.getMinutes();

// creating month's name array to display name of month
var monthArr = new Array();
monthArr = ['Jan','Feb','March','April','May']; // you can add all months

// Code to show time as per requirement
var ampm = h >= 12 ? 'pm' : 'am';
h = h % 12;
hours = h ? h : 12; // the hour '0' should be '12'
minutes = min < 10 ? '0'+min : min;

var strTime = hours + '.' + minutes + ' ' + ampm;

var finalDate = monthArr[m]+' '+d+','+y+', '+strTime;
console.log(finalDate);

I hope this helps you.

VincenzoC
  • 24,850
  • 12
  • 71
  • 90
0

As you stated the problem, I had a solution, tweak it as you want. Also try this Fiddle DEMO

function dateFormater(dateStr, format, separator) {
    var date = new Date(dateStr),
        formatArr = format.split('-'),
        len = formatArr.length,
        str = '',
        i,
        monthNames = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
        ],
        getdata = function(d) {
            function getCurrentTime(d) {
                var currentTime,
                    hour = d.getHours(),
                    meridiem = hour >= 12 ? "PM" : "AM";
                return ((hour + 11) % 12 + 1) + ":" + d.getMinutes() + ' ' + meridiem;
            }
            switch (d) {
                case 'day':
                    return getDay(date.getDay);
                case 'month':
                    return monthNames[date.getMonth()];
                case 'dd':
                    return date.getDate();
                case 'mm':
                    return date.getMonth();
                case 'yy':
                    return date.getFullYear();
                case 'time':
                    return getCurrentTime(date);
                case 'zone':
                    return date.toString().match(/\(([A-Za-z\s].*)\)/)[1];
            }
        },
        getDay = function(d) {
            switch (d) {
                case 0:
                    return "Sunday";
                case 1:
                    return "Monday";
                case 2:
                    return "Tuesday";
                case 3:
                    return "Wednesday";
                case 4:
                    return "Thursday";
                case 5:
                    return "Friday";
                case 6:
                    return "Saturday";
            }
        };

    for (i = 0; i < len; i++) {
        i === len - 1 && (separator = '');
        str += getdata(formatArr[i]) + separator;
    }

    return str;
}

var format = 'month-dd-yy-time-zone';
console.log(dateFormater("24 May 2017, 05:35", format, ', ')); // May, 24, 2017, 5:35 AM, IST
Sumit
  • 1,505
  • 2
  • 9
  • 16