1

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

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

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

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

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

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/2016 and then send it to the backend. I tried some other functions too which are available in javascript, like:

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!

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • `#...#` is a vb representation for a `DateTime`. – Daniel A. White Aug 12 '16 at 18:35
  • Ya true. So shall I send just mm/dd/yyyy format. if so, how can I convert my `start = 2016-08-26T03:59:59.999Z ` ? Please help me. – Unbreakable Aug 12 '16 at 18:37
  • 2
    Possible duplicate of [Format date to MM/dd/yyyy in javascript](http://stackoverflow.com/questions/11591854/format-date-to-mm-dd-yyyy-in-javascript) – Steven B. Aug 12 '16 at 18:41
  • @lamelemon: I checked that link but I could not find method like getMonth() on the `start`. So I am not able to do soemthing like `start.getMonth()` it gives me error in javascript. Please guide. – Unbreakable Aug 12 '16 at 18:43
  • 1
    @Unbreakable how are you actually getting the date? [Demo](https://jsfiddle.net/q14p1b7x/) – Steven B. Aug 12 '16 at 18:47
  • It would be better to change the backend to use dates in a yyyy-MM-dd format: it is understood unambiguously around the world. – Andrew Morton Aug 13 '16 at 13:17

1 Answers1

1

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:

        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);
ModusPwnens
  • 165
  • 3
  • 12
  • 1
    If this is the answer then I think this is a duplicate of: http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – ModusPwnens Aug 12 '16 at 19:09