0

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
Codded
  • 1,256
  • 14
  • 42
  • 74
  • 1
    Look at this blog article. It explains a lot on how to format Date in javascript. It have what you need now and also other format that maybe useful down the road. http://blog.stevenlevithan.com/archives/date-time-format This StackOverflow article also have something similar to what you need. http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript – Steven Sep 17 '12 at 12:20
  • @Steven That blogpost recommends using a date formatting module embedded into the post. Surely there is a better builtin way? – Josiah Yoder Feb 16 '23 at 22:04

4 Answers4

4

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);
Diode
  • 24,570
  • 8
  • 40
  • 51
1

Try with following code.

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

$( "#datepicker" ).datepicker( "D M yy", dflip.val());
Nitesh Kumar
  • 1,774
  • 4
  • 19
  • 26
1

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; 
}
LouieT
  • 25
  • 1
  • 8
0

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

Jim Davis
  • 1,230
  • 6
  • 11