admin管理员组

文章数量:1278819

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

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
Share Improve this question asked Sep 17, 2012 at 12:18 CoddedCodded 1,25614 gold badges43 silver badges74 bronze badges 2
  • 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. blog.stevenlevithan./archives/date-time-format This StackOverflow article also have something similar to what you need. stackoverflow./questions/1056728/… – Steven Commented Sep 17, 2012 at 12:20
  • @Steven That blogpost remends using a date formatting module embedded into the post. Surely there is a better builtin way? – Josiah Yoder Commented Feb 16, 2023 at 22:04
Add a ment  | 

4 Answers 4

Reset to default 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);

Try with following code.

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

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

This may not be the cleanest, most efficient or best way to acplish 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; 
}

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

http://depressedpress./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).

本文标签: Javascript date format without timezoneStack Overflow