-1

My question is how can get from a date like: mm/dd/yyyy to Saturday 26 april 2014?

Within the div I've a date as rel,

<td class="td-time" rel="06/02/14">09.00</td>

Js

var firstRowDate = $('.td-time:first-child').attr('rel');
var date = new Date(firstRowDate);

I did try like this:

var n = firstRowDate.getDay();

But I get a error: undefined is not a function

STP38
  • 348
  • 2
  • 14
  • 2
    Try [Date.js](http://www.datejs.com/). It makes handling dates in JS much easier, and has an i18n implementation. – Rory McCrossan Jun 02 '14 at 11:42
  • @Pete No it isn't a duplicate – STP38 Jun 02 '14 at 11:47
  • [moment.js](http://www.momentjs.com) is another good date/time library. – h2ooooooo Jun 02 '14 at 11:47
  • @STP38 have a look through the answers, there are many libraries they suggest for you to use to get the format of the date you want, but the problem with your code above is you are doing getDay on a text var not on the `date` var – Pete Jun 02 '14 at 11:51
  • @Pete So my question isn't a duplicate :-) – STP38 Jun 02 '14 at 11:53
  • well the first line of your question is wrong then as it's a duplicate of what your question is – Pete Jun 02 '14 at 11:54

5 Answers5

1

If your date object is a valid date then,

var firstRowDate = $('.td-time:first-child').attr('rel');
var date = new Date(firstRowDate);
var dateString = date.toDateString();

You should have looked at http://www.w3schools.com/jsref/jsref_obj_date.asp . List of all the date js functions you need.

jsfiddle to try out: http://jsfiddle.net/Grimbode/sn6hQ/

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
1

If you have a string in month/day/year format, you can use a function like the following to convert it to day-name date month-name year format:

// s is date in m/d/y format
function convertMDYstring(s) {

  var b = s.split(/\D/);
  var d = new Date(b[2],--b[0],b[1]);

  // Replace with day names in appropriate language
  var days = ['Sunday','Monday','Tuesday','Wednesday',
              'Thursday','Friday','Saturday'];

  // Replace with month names in appropriate language
  var months = ['January','February','March','April','May','June','July',
                'August', 'September','October','November','December'];

  // Helper for padding single digit values
  function z(n){return (n<10? '0' : '') + n;}

  return days[d.getDay()] + ' ' + d.getDate() + ' ' +
         months[d.getMonth()] +  ' ' + d.getFullYear();
}
RobG
  • 142,382
  • 31
  • 172
  • 209
0
 var d = new Date("1/05/2014");
 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];<br>
 var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];<br>
 var date =days[d.getDay()]+" "+ d.getDate() + " " + months[d.getMonth()] + " " + d.getFullYear();

 alert(date);
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
Ravi
  • 1,320
  • 12
  • 19
0

In your code, you are doing something wrong:

I did try like this:

var n = firstRowDate.getDay();

Here the firstRowDate is not a date time variable/object.

You should try like that -

var n = date.getDay()

Demo

And according your question title:

var dd = new Date($('.td-time').attr('rel'));
var weekday = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var curMonth = months[dd.getMonth()];
var dayOfWeek = weekday[dd.getDay()];
alert(dayOfWeek + " " + dd.getDate() + " " + curMonth + " " + dd.getFullYear());

Working Example

Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
0

You can convert Date to almost any format using the Snippet I have added below.

For your Case you need to execute this:

Code:

dateFormat(new Date(),"dddd d mmmm yyyy")

Output

Monday 2 June 2014"

Other example

// Can also be used as a standalone function
dateFormat(new Date(), "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM

Snippet:

Add following code taken from this link into your code.

var dateFormat = function () {
    var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
        timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
        timezoneClip = /[^-+\dA-Z]/g,
        pad = function (val, len) {
            val = String(val);
            len = len || 2;
            while (val.length < len) val = "0" + val;
            return val;
        };

    // Regexes and supporting functions are cached through closure
    return function (date, mask, utc) {
        var dF = dateFormat;

        // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
        if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
            mask = date;
            date = undefined;
        }

        // Passing date through Date applies Date.parse, if necessary
        date = date ? new Date(date) : new Date;
        if (isNaN(date)) throw SyntaxError("invalid date");

        mask = String(dF.masks[mask] || mask || dF.masks["default"]);

        // Allow setting the utc argument via the mask
        if (mask.slice(0, 4) == "UTC:") {
            mask = mask.slice(4);
            utc = true;
        }

        var _ = utc ? "getUTC" : "get",
            d = date[_ + "Date"](),
            D = date[_ + "Day"](),
            m = date[_ + "Month"](),
            y = date[_ + "FullYear"](),
            H = date[_ + "Hours"](),
            M = date[_ + "Minutes"](),
            s = date[_ + "Seconds"](),
            L = date[_ + "Milliseconds"](),
            o = utc ? 0 : date.getTimezoneOffset(),
            flags = {
                d:    d,
                dd:   pad(d),
                ddd:  dF.i18n.dayNames[D],
                dddd: dF.i18n.dayNames[D + 7],
                m:    m + 1,
                mm:   pad(m + 1),
                mmm:  dF.i18n.monthNames[m],
                mmmm: dF.i18n.monthNames[m + 12],
                yy:   String(y).slice(2),
                yyyy: y,
                h:    H % 12 || 12,
                hh:   pad(H % 12 || 12),
                H:    H,
                HH:   pad(H),
                M:    M,
                MM:   pad(M),
                s:    s,
                ss:   pad(s),
                l:    pad(L, 3),
                L:    pad(L > 99 ? Math.round(L / 10) : L),
                t:    H < 12 ? "a"  : "p",
                tt:   H < 12 ? "am" : "pm",
                T:    H < 12 ? "A"  : "P",
                TT:   H < 12 ? "AM" : "PM",
                Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
                o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
                S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
            };

        return mask.replace(token, function ($0) {
            return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
        });
    };
}();

// Some common format strings
dateFormat.masks = {
    "default":      "ddd mmm dd yyyy HH:MM:ss",
    shortDate:      "m/d/yy",
    mediumDate:     "mmm d, yyyy",
    longDate:       "mmmm d, yyyy",
    fullDate:       "dddd, mmmm d, yyyy",
    shortTime:      "h:MM TT",
    mediumTime:     "h:MM:ss TT",
    longTime:       "h:MM:ss TT Z",
    isoDate:        "yyyy-mm-dd",
    isoTime:        "HH:MM:ss",
    isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
    isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
    dayNames: [
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    ],
    monthNames: [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
        "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
    ]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
    return dateFormat(this, mask, utc);
};
Harpreet Singh
  • 2,651
  • 21
  • 31