Chinmay235
Chinmay235

Reputation: 3414

JavaScript DateTime not working

function formatDate(dt) {
    //var date = new Date(dt);
    var date = new Date('2015-08-27 16:00:00'); alert(date.getMonth());
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0' + minutes : minutes;
    var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
    return date.getDate() + " " + date.getMonth() + " " + date.getFullYear() + " " + strTime;
}

I have tried to fetch Date and time. But I am getting NaN while alert date.getMonth();. If I am removing time then this is working fine. But My date-time format dynamic. This is coming from the database like 0000-00-00 00:00:00.

I want to view my database date and time in the 27 Aug 2015 04:00:00 am/pm format.

Upvotes: 0

Views: 4772

Answers (7)

Sarjan Desai
Sarjan Desai

Reputation: 3733

The date format you are using (2015-08-27 16:00:00) is not the proper format for Firefox, though it works in Chrome. So, for this code to work properly on all browsers, it should not be used.

The below code works in Firefox and Chrome:

I've replaced the string variable date - with /. This format works for both Firefox and Chrome.

Another format that works in Firefox and Chrome is 1995-12-17T03:24:00 which includes T instead of ' ' (space).

However, the above format gives different value in Chrome and Firefox.

new Date('2015-10-05T03:24:00'); // Returns Mon Oct 05 2015 08:54:00 GMT+0530 (IST) in Chrome

new Date('2015-10-05T03:24:00'); // Returns 2015-10-04T21:54:00.000Z in Firefox

var date1 = '2015-08-20 09:38:20';
var date1Updated = new Date(date1.replace(/-/g,'/'));
alert(date1Updated.getMonth());

Upvotes: 2

Gupta Nambula
Gupta Nambula

Reputation: 121

 var strDate =  addZero(d.getDate()) + "/" + addZero((d.getMonth() + 1))+"/"  +d.getFullYear();
        alert("strDate :"+strDate)
        return strDate;
    }
    function addZero(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }

Upvotes: 1

RobG
RobG

Reputation: 147343

Parsing strings using the Date constructor is largely implementation dependent. Only one format of string is specified as being supported by the specification and that changed to some extent between ES5 and ECMAScript 2015.

Your best option is to manually parse the string, either using your own function or a library. The following will suit if the string is consistently the format in the OP and the timezone is UTC:

/*  parse dates of format yyyy-mm-dd hh:mm:ss 
**  e.g. 2015-08-27 16:00:00
** 
**  @param {string} s - Date string in format yyyy-mm-dd hh:mm:ss
**  @returns {Date}   - String as Date assuming UTC
**
**  Does not validate that the string is valid date or time
**/
function parseDate (s) {
  var b = s.split(/\D/);
  return new Date(Date.UTC(b[0], b[1]-1, b[2], b[3], b[4], b[5]));
}

document.write(parseDate('2015-08-27 16:00:00'));

Upvotes: 0

Danijel Duric
Danijel Duric

Reputation: 1

Your code doesn't work in firefox

In firefox 2015-08-27 16:00:00isn't valid. To be valid it has to be 2015-08-27T16:00:00. To make it valid in firefox, the easiest solution would be

function formatDate(dt) {
    //var date = new Date(dt);
    var sampleDate = "2015-08-27 16:00:00"; // Your sample date as string
    var another = sampleDate.replace(' ', 'T');// Change here. Replaced the empty space with the 'T' to make it work in firefox
    var date = new Date(another); alert(date.getMonth()); // Using your date to create new Date object
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0' + minutes : minutes;
    var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
    return date.getDate() + " " + date.getMonth() + " " + date.getFullYear() + " " + strTime;
}

Hope i was helpfull

Upvotes: 0

İlker Korkut
İlker Korkut

Reputation: 3240

You have an invalid date format, It seems Chrome handle this situation but firefox not.

 new Date('2015-08-27 16:00:00') // Invalid format
 new Date('2015-08-27T16:00:00') // Correct Format With T

Upvotes: 0

Bobby Tables
Bobby Tables

Reputation: 3013

In firefox '2015-08-27 16:00:00' is an invalid date.

Your options are

var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);

In your case you're missing the T before the time

Documentation

Upvotes: 0

ozil
ozil

Reputation: 7117

The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.

Note: January is 0, February is 1, and so on.
you need to add one like getMonth() + 1.

function formatDate(dt) {
    //var date = new Date(dt);
    var date = new Date('2015-08-27 16:00:00'); 
    //alert(date.getMonth() + 1);
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0' + minutes : minutes;
    var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
    return date.getDate() + " " + (date.getMonth() + 1) + " " + date.getFullYear() + " " + strTime;
}
alert(formatDate());

Upvotes: 0

Related Questions