2

i am trying to convert milliseconds to my desired date format. But the format that i give to the function doesn`t seem to work.

In my map function

var date = new Date(item.showTime);
var dateString = date.toString('MM/dd/yy HH:mm:ss');
  emit([doc.vehicleNumber,doc.advertId],{"seatNumber":item.seatNumber,"showTime":dateString ,"skipTime":item.skipTime});
});

The result is

{seatNumber: 2, showTime: "Tue Nov 21 2017 10:08:56 GMT+0000 (UTC)", skipTime: 0}

I need show time to in format of 10/12/2017 10:08:56.. I don`t know why this is not working.

Btw this is not javascript, i think it is about couchdb so please do not mark this as duplicate with other JS questions.

rematnarab
  • 1,147
  • 2
  • 15
  • 38
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – ktiu Dec 13 '17 at 12:52
  • I specifically wrote... Btw this is not javascript, i think it is about couchdb so please do not mark this as duplicate with other JS questions. @ktiu – rematnarab Dec 13 '17 at 13:08

3 Answers3

2

CouchDB supports the use of CommonJS Modules in the map function definition. http://docs.couchdb.org/en/2.1.1/query-server/javascript.html#commonjs

The problem is that modules should be defined in the design document and can not be loaded from external resources.

You can use standard JavaScript built-in objects and functions in your map function as couchjs is based in Mozilla's SpiderMonkey JS interpreter. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

There is not base support in CouchDB JS runtime for date formating. You should write your own logic for this purpose.

If this is a big issue for you, you may try to hack the /path/to/couchdb/share/server/main.js file which is the one that sets the execution contexts of your functions, but I don't see it too much recommendable.

Juanjo Rodriguez
  • 1,894
  • 6
  • 17
0

Parse the date and format it yourself. It's not difficult.

var date = new Date(Date.parse(item.showTime));
var timestring = "" + date.getMonth() + "/" + date.getDate() + "/" + 
    date.getFullYear() + " " + date.toTimeString().substr(0,8)
xpqz
  • 2,793
  • 8
  • 12
-1

This is very much about JavaScript. The .toString() method does not take any formatting parameters. You can write your own function to convert the output, or you can use a library that does what you want.

ktiu
  • 136
  • 7
  • Thanks for the answer. I don`t want to write my own function for such an easy task.. I am looking a way that couchDB handles this. And i don`t think i can import external JS library to couchDB – rematnarab Dec 13 '17 at 15:13