Javascript 没有时区的Javascript日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12459017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 08:01:17  来源:igfitidea点击:

Javascript date format without timezone

javascriptdate

提问by Codded

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

我有一个从输入值定义的 javascript 变量。

$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

回答by Diode

You can do something using substringor toDateStringor both

您可以使用substringortoDateString或两者来做某事

for e.g:

例如:

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

回答by Nitesh

Try with following code.

尝试使用以下代码。

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

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

回答by LT-Bienabee

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.

这可能不是完成您正在寻找的最干净、最有效或最好的方法,但我创建了一个函数来返回没有时区的日期。您可以调整“theDate”变量以仅返回您想要的日期部分。

    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; 
}

回答by Jim Davis

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

我的 DateExtentions 库会做到这一点 - 尽管如果您所做的只是那种简单的格式,它可能会有点过分。

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

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

我可以根据传递的掩码解析日期并根据您的喜好格式化输出(它还可以执行各种日期数学和实用程序,因此它可能比您需要的更重)。