Codded
Codded

Reputation: 1256

Javascript date format without timezone

I have a javascript variable which is defined from an input value.

$d = $('#date').val();
$myDateParts = $d.split("-");
$dflip = new Date($myDateParts[2], ($myDateParts[1]-1), $myDateParts[0]);
console.log($dflip);

$dflip = Wed Sep 19 00:00:00 UTC+0100 2012 

How can i format the output to just:

Wed Sep 19

Upvotes: 0

Views: 28592

Answers (4)

LouieT
LouieT

Reputation: 25

This may not be the cleanest, most efficient or best way to accomplish what you're looking for, but I created a function to return the date without the timezone. You can adjust the "theDate" variable to return only the parts of the date you want.

    function properDate(){  

    var d = new Date();

    var DayOfMonth = d.getDate();
    var DayOfWeek = d.getDay();
    var Month = d.getMonth();
    var Year = d.getFullYear();
    var Hours = d.getHours();
    var Minutes = d.getMinutes();
    var Seconds = d.getSeconds();

    switch (DayOfWeek) {
    case 0:
        day = "Sun";
        break;
    case 1:
        day = "Mon";
        break;
    case 2:
        day = "Tue";
        break;
    case 3:
        day = "Wed";
        break;
    case 4:
        day = "Thu";
        break;
    case 5:
        day = "Fri";
        break;
    case 6:
        day = "Sat";
        break;
    }

    switch (Month) {
    case 0:
        month = "Jan";
        break;
    case 1:
        month = "Feb";
        break;
    case 2:
        month = "Mar";
        break;
    case 3:
        month = "Apr";
        break;
    case 4:
        month = "May";
        break;
    case 5:
        month = "Jun";
        break;
    case 6:
        month = "Jul";
        break;
    case 7:
        month = "Aug";
        break;
    case 8:
        month = "Sep";
        break;
    case 9:
        month = "Oct";
        break;
    case 10:
        month = "Nov";
        break;
    case 11:
        month = "Dec";
        break;
    }




    var theDate = day + " " + month + " " + DayOfMonth + "  " + Year + " " + Hours + ":" + Minutes + ":" + Seconds;

    return theDate; 
}

Upvotes: 1

Nitesh Kumar
Nitesh Kumar

Reputation: 1774

Try with following code.

<script src="../../ui/jquery.ui.datepicker.js"></script>

$( "#datepicker" ).datepicker( "D M yy", dflip.val());

Upvotes: 1

Jim Davis
Jim Davis

Reputation: 1230

My DateExtentions library will do that - although it may be overkill if all you're doing is that one, simple format.

http://depressedpress.com/javascript-extensions/dp_dateextensions/

I can parse the date based on a passed mask and format the output however you like (it'll also do all sorts of date math and utility stuff so again, it might be heavier than you need).

Upvotes: 0

Diode
Diode

Reputation: 25135

You can do something using substring or toDateString or both

for e.g:

var dateString = new Date(2012, 0, 31).toDateString();
var noYear = dateString.substring(0, dateString.length-5);
console.log(noYear);

Upvotes: 4

Related Questions