最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting humanized strings to dates moment.js - Stack Overflow

programmeradmin0浏览0评论

Moment.js does humanized and calendar dates, for example:

moment().calendar()
"Today at 10:17 AM"

which gets a current date object and converts to a calendar date, and

is there any way to do that in reverse? Like if I give it "Today at 10:17 AM", it returns a date object with todays date and 10:17 AM as the time?

Moment.js does humanized and calendar dates, for example:

moment().calendar()
"Today at 10:17 AM"

which gets a current date object and converts to a calendar date, and

is there any way to do that in reverse? Like if I give it "Today at 10:17 AM", it returns a date object with todays date and 10:17 AM as the time?

Share Improve this question edited May 9, 2013 at 15:27 Agustin Meriles 4,8543 gold badges31 silver badges44 bronze badges asked May 9, 2013 at 15:24 user864572user864572 1913 silver badges13 bronze badges 3
  • similar question here: stackoverflow./questions/1003326/… – arturomp Commented May 9, 2013 at 15:30
  • Yeah, sorry, didn't search natural language. Though the answer there (and the only reply here) doesn't handle day + time – user864572 Commented May 9, 2013 at 17:11
  • absolutely! didn't mean that in a bad way, I just thought it'd be nice to know about it given the number of contributions ;) – arturomp Commented May 9, 2013 at 17:54
Add a ment  | 

2 Answers 2

Reset to default 9

date.js can parse human readable dates:

http://www.datejs./

SugarJS can also parse natural language dates:

http://sugarjs./dates

It can deal with stuff like:

one day before yesterday
2 days after monday
2 weeks from monday
a second ago
25 years from now
last wednesday

Also, see this related question: Is there a natural language parser for date/times in javascript?

Chrono v2

It's the only library I've found that also parses timezones.

Examples


Unfortunately, the only documented way of using is via NPM or as ES6 Module, but I found a way to use the library with the traditional script tag approach.

Non-NPM / non-module usage:

  1. Include this script: https://www.unpkg./chrono-node/dist/bundle.js
  2. Use the global variable chrono in your script (available methods here)

E.g. chrono.parseDate('Tomorrow at 4 PM PST')

发布评论

评论列表(0)

  1. 暂无评论