Javascript日期转换、排序和显示
我有一串数据需要使用 javascript 在前端进行转换、排序和显示。
数据采用此格式
yyyy-mm-dd hh:mm:ss 此处为活动标题
提要示例:
2011-04-19 00:00:00 event1 标题在此
2011-04-22 00:00:00 event2 标题在此
2011-05-25 00:00:00 event3 标题在此
2011-04-13 00:00:00 event4 标题在此
2011-12-13 00:00:00 event5 标题在这里
需要它转换日期格式,按日期排序显示,省略当前日期之前的事件(当前日期为 04-21-2011):
所需列表的示例:
2011 年 4 月 22 日活动 2 标题在此
2011 年 5 月 25 日活动 3 标题在此
2011 年 12 月 13 日 event5 标题在此
提前致谢
I have a string of data that I need to convert, sort and display on the frontend using javascript.
Data is in this format
yyyy-mm-dd hh:mm:ss event title here
Example of feed:
2011-04-19 00:00:00 event1 title here
2011-04-22 00:00:00 event2 title here
2011-05-25 00:00:00 event3 title here
2011-04-13 00:00:00 event4 title here
2011-12-13 00:00:00 event5 title here
Need it to convert date format, display sorted by date, omitting events prior to current date (current date being 04-21-2011):
Example of desired list:
04-22-2011 event2 title here
05-25-2011 event3 title here
12-13-2011 event5 title here
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是你要我们做的家庭作业吗:P,给我一些时间,我会帮你做点什么。
现在这只是一种对日期进行排序的方法,请使用此逻辑来适应您的需要。
您可以做几件事,可以为您的活动构建对象,以便在排序过程中将它们与日期联系起来。
来自 about.com 的日期排序
Is this a homework you are getting us to do :P, give me some time I'll hook you up with something.
Now this is only a way to sort date, use this logic to adapt it for your need.
There are a couple of things you could do, possibly building object for your event to keep them tied with the dates as the sort goes through.
Date sort from about.com
在这里制作一些插件,但它们可能会有所帮助:
日期解析和操作可以由我的 DateExtensions 组件。日期列表的管理(排序)可以通过我的 ObCollectionOrdered 组件轻松处理。
两者都有一些例子应该会有所帮助。
根据具体情况,他们可能会使用大锤来杀死苍蝇,但他们会为您从问题中抽象出几乎所有的复杂性。
Making some plugs here, but they may help:
The date-parsing and manipulation can be handled by my DateExtensions component. The management of the list of dates (sorting) would be easily handled by my ObCollectionOrdered component.
There are examples of both that should help.
Depending on the circumstances these may be using a sledge-hammer to kill a fly, but they will abstract nearly all the complexity away from the problem for you.
只需告诉您的程序员停止玩 3D 射击游戏并做好他们的工作即可^^
开玩笑:
因为您输入字符串的日期部分似乎遵循 ISO8601 格式,另一种解决方案是直接按字面顺序对整个字符串进行排序(可能是因为在这种情况下,时间顺序与字母顺序相同),但这是另一个故事,我不习惯它。
Just tell your coders to stop playing 3D shooters and do their jobs^^
Joking aside:
As the date parts of your input strings seems to follow ISO8601 format, another solution would be to directly sort the whole strings literally (possible because chronological order would be the same as alphabetic order in this case), but that's another story and I'm not used to it.
听起来您需要实现一个执行以下任务的函数:
解析输入:必须将字符串解析为具有“日期”和“标题”属性的对象列表。尝试将“YYYY-MM-DD”日期格式与正则表达式匹配,并使用其构造函数构建一个 Date 对象。
过滤数据:如果早于当前时间,则应拒绝对象,并按日期升序排列(从最旧到最新)。可以在 JavaScript 中对日期进行比较,并且可以对列表进行本地排序。
格式化输出:对象应作为具有所需日期格式的字符串列表返回。尝试通过从月份、日期和年份获取器构建字符串来输出日期。
当然,您可以使用库来执行各种任务,例如日期处理,但使用纯 JavaScript 应该足够简单。例如:
It sounds like you need to implement a function which performs these tasks:
Parsing the input: the string must be parsed into a list of objects with "date" and "title" properties. Try matching the "YYYY-MM-DD" date format with a regular expression and building a Date object with its constructor.
Filtering the data: the objects should be rejected if older than the current time and sorted by date in increasing order (oldest to newest). Dates can be compared and lists can be sorted natively in JavaScript.
Formatting the output: the objects should be returned as a list of strings with the desired date format. Try outputting the date by building a string from its month, date, and year getters.
You could, of course, use libraries for various tasks like date handling but using pure JavaScript should be simple enough. For example: