简体   繁体   English

在 JavaScript 中从不同的字符串中提取不同格式的日期

[英]Extracting dates in different formats from different strings in JavaScript

I am writing a node.JS application to parse google takeout data from google photos, for EXIF data.我正在编写一个 node.JS 应用程序来解析来自谷歌照片的谷歌外卖数据,用于 EXIF 数据。

I am not good at regex, and have tried sites such as regexr.com to no avail, since there are a lot of unknowns.我不擅长正则表达式,并且尝试过诸如 regexr.com 之类的网站都无济于事,因为有很多未知数。 But I am wondering if there is a more efficient way of doing this?但我想知道是否有更有效的方法来做到这一点?

Say I could have various potential strings说我可以有各种潜在的字符串

"IMG_20150628_184721-ANIMATION" # There is a date + time within, but they are not in an ISO format, also non-regular characters "Screenshot_2015-06-27-22-51-00" # Has a date/time at the end, but also has useless string within it "2015-06-28" # Cleanly formatted date without time "2015-06-28 22:51:05" # Cleanly formatted date and time "2015-06-28 #1" # Date + Space and Extra characters that I don't want "2015-06-28 #3 " # Date + Space and extra characters that I don't want and a trailing space "2015-06-18-19" # Date + An extra number (happens to be the next day) "NoDateOrTimeInThisString" # No Date "IMG_1234" # No date

This list is non exhaustive, and there may be other strings before+after dates.此列表并非详尽无遗,日期之前+之后可能还有其他字符串。 It is also possible that a date/time will not actually be in the string日期/时间也可能实际上不在字符串中

I don't have any way of knowing which of these particular file names I am going to end up with, and within my knowledge scope I don't know how I would be able to extract the dates from each of these.我没有任何办法知道我最终会得到哪些特定的文件名,而且在我的知识范围内,我不知道如何从每个文件名中提取日期。

Does anyone have any idea how i'd go about doing this in JavaScript?Such as a library?有谁知道我将如何在 JavaScript 中执行此操作?例如库? If possible i'd like to be able to get it into a JS Date/Time object.如果可能,我希望能够将其放入 JS 日期/时间对象中。 But I have no idea how i'd go about this.但我不知道我会怎么做。

You might want to have a look at Moment.js .你可能想看看Moment.js

Also, regexes deserve a whole tutorial by themselves, they're a language of their own.此外,正则表达式本身值得一整个教程,它们是自己的语言。 Specifically, look at capturing groups, whihc allow you to extraxt certain parts of the matched expression.具体来说,看看捕获组,它允许您提取匹配表达式的某些部分。

What assumptions are you allowed to make on the input?您可以对输入做出哪些假设? Will there always be a sequence of yyyy(...)mm(...)dd hidden in it somewhere?总会有一个 yyyy(...)mm(...)dd 序列隐藏在它的某个地方吗?

You could try something like你可以尝试类似的东西

.*([0-9]{4}).*([0-9]{2}).*([0-9]{2}).*

(Haven't tried the expression yet, may need some tweaking.) (还没有尝试过这个表达式,可能需要一些调整。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM