vb.net 如何转换格式为 mm/dd/yyyy 的日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38924416/
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-09-17 20:01:20  来源:igfitidea点击:

How to convert date time in format mm/dd/yyyy

javascriptvb.net

提问by Unbreakable

My VB.netRest service at backend expects date in below format:

VB.net在后端的 Rest 服务需要以下格式的日期:

 starterInput.dateProp = 08/26/2016 where dateProp is of date type

Currently I am getting date in below format in my front endJavascript

目前我在前端获取以下格式的日期Javascript

start = 2016-08-26T03:59:59.999Z 

How can I convert my date of 2016-08-26T03:59:59.999Zto 08/26/2016in Javascript

如何在 Javascript 中将我的日期转换2016-08-26T03:59:59.999Z08/26/2016

I have tried some of the built in function.

我尝试了一些内置功能。

start.toArray()gives me something like [2016, 7, 10, 3, 59, 59, 0]So shall I parse this array and use the index to create something like 08/26/2016and then send it to the backend. I tried some other functions too which are available in javascript, like:

start.toArray()给我这样的东西[2016, 7, 10, 3, 59, 59, 0]所以我应该解析这个数组并使用索引来创建类似的东西 08/26/2016,然后将它发送到后端。我也尝试了一些在 javascript 中可用的其他功能,例如:

start.format()

output: "2016-08-10T03:59:59+00:00"

start.toString()

output: ""Wed Aug 08 2016 03:59:59 GMT+0000"

I am confused how to get the date in the format I expect 08/26/2016. Please guide me. Thanks!

我很困惑如何以我期望的格式获取日期08/26/2016。请指导我。谢谢!

采纳答案by ModusPwnens

If you're just trying to convert the JavaScript date object to a string like the following: 08/26/2016 then you can do as follows:

如果您只是想将 JavaScript 日期对象转换为如下所示的字符串:08/26/2016 那么您可以执行以下操作:

        function getFormattedDate(date) {
            var year = date.getFullYear();
            /// Add 1 because JavaScript months start at 0
            var month = (1 + date.getMonth()).toString();
            month = month.length > 1 ? month : '0' + month;
            var day = date.getDate().toString();
            day = day.length > 1 ? day : '0' + day;
            return month + '/' + day + '/' + year;
        }

        var formattedStart = getFormattedDate(start);