admin管理员组

文章数量:1278786

In the same spirit as discussed here, is there a remended way to generate / parse dates from within a bash script so that it can be interfaced to Javascript Date?

To be precise, I get this strings when doing json encoding of a Javascript Date object:

2011-10-31T10:23:47.278Z

I could put together a bash hack to generate / parse that date format, but I would prefer to avoid reinventing the wheel. Does somebody have a working solution?

I am more interested in the "generating" side: I want to generate current dates from a bash script and save them in a json document (couchdb) so that they can be automatically ordered by the view engine.

In the same spirit as discussed here, is there a remended way to generate / parse dates from within a bash script so that it can be interfaced to Javascript Date?

To be precise, I get this strings when doing json encoding of a Javascript Date object:

2011-10-31T10:23:47.278Z

I could put together a bash hack to generate / parse that date format, but I would prefer to avoid reinventing the wheel. Does somebody have a working solution?

I am more interested in the "generating" side: I want to generate current dates from a bash script and save them in a json document (couchdb) so that they can be automatically ordered by the view engine.

Share Improve this question edited May 23, 2017 at 10:30 CommunityBot 11 silver badge asked Nov 3, 2011 at 2:32 blueFastblueFast 44.5k66 gold badges213 silver badges372 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

The closest I am ing is this:

date -u +"%FT%T.000Z"

Which gives this output:

2011-11-03T06:43:08.000Z

I do not like that I have to put the T, the Z and the milliseconds to 0 manually (I can use %N for nanoseconds, and truncate with sed or whatever, but seems like overkill just to get millisecond precission), and I was hoping that there would be a built-in format token for date which would produce that UTC date. I assumed - wrongly it seems - that the format is mon enough that it can be specified with just one format token.

JavaScript can convert many different values into dates. Not sure if that's what you mean, but for example. Your bash could generate this string: "2011/11/10 08:08:08"

When it gets to JavaScript land you can do this

var date = new Date("2011/11/10 08:08:08")

You can also do this:

var now = 1320287813362
var date = new Date(now)

More info on what Date accepts here: https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Date

Other interesting info here: What's the best way to store datetimes (timestamps) in CouchDB?

本文标签: JSON datetime between bash script and JavaScriptStack Overflow