Javascript:从字符串中提取日期?

发布于 2024-12-06 08:21:27 字数 191 浏览 0 评论 0原文

我有一个字符串格式为

Today 3:28AM
Yesterday 3:28AM
08/22/2011 3:28AM

我需要做的是以某种方式将字符串的日期部分提取到变量中,即。 “今天”、“昨天”或格式为 DD/MM/YYYY 的日期。

Javascript 可能实现这样的事情吗?

I have a string formatted as either

Today 3:28AM
Yesterday 3:28AM
08/22/2011 3:28AM

What I need to do is somehow extract into a variable the date portion of my string, ie. 'Today', 'Yesterday' or a date formatted as DD/MM/YYYY.

Is something like this possible at all with Javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

后知后觉 2024-12-13 08:21:27

由于 JavaScript 日期解析器无法识别您的日期,因此您可以编写一个解析器,将日期转换为它可以识别的格式。下面是一个函数,它采用您提供的日期示例并格式化它们以获得有效的日期字符串:

function strToDate(dateStr) {
    var dayTimeSplit = dateStr.split(" ");  
    var day = dayTimeSplit[0];
    var time = dayTimeSplit[1];

    if (day == "Today") {
        day = new Date();
    } else if (day == "Yesterday") {
        day = new Date();
        day.setDate(day.getDate() - 1);
    } else {
        day = new Date(day);
    }

    var hourMinutes = time.substring(0, time.length -2);
    var amPM = time.substring(time.length -2, time.length);

    return new Date((day.getMonth() + 1) + "/" + day.getDate() + "/" + day.getFullYear() 
        + " " + hourMinutes  + " " + amPM);
}

然后您可以调用 stroToDate 将日期格式转换为有效的 JavaScript 日期:

console.log(strToDate("Today 3:28AM")); 
console.log(strToDate("Yesterday 3:28AM"));
console.log(strToDate("08/22/2011 3:28AM"));

输出:

Sun Sep 25 2011 03:28:00 GMT-0700 (Pacific Daylight Time)
Sat Sep 24 2011 03:28:00 GMT-0700 (Pacific Daylight Time)
Mon Aug 22 2011 03:28:00 GMT-0700 (Pacific Daylight Time)

Since the JavaScript date parser won't recognize your dates, you can write a parser that puts the date into a format that it will recognize. Here is a function that takes the date examples that you gave and formats them to get a valid date string:

function strToDate(dateStr) {
    var dayTimeSplit = dateStr.split(" ");  
    var day = dayTimeSplit[0];
    var time = dayTimeSplit[1];

    if (day == "Today") {
        day = new Date();
    } else if (day == "Yesterday") {
        day = new Date();
        day.setDate(day.getDate() - 1);
    } else {
        day = new Date(day);
    }

    var hourMinutes = time.substring(0, time.length -2);
    var amPM = time.substring(time.length -2, time.length);

    return new Date((day.getMonth() + 1) + "/" + day.getDate() + "/" + day.getFullYear() 
        + " " + hourMinutes  + " " + amPM);
}

Then you can call stroToDate to convert your date formats to a valid JavaScript Date:

console.log(strToDate("Today 3:28AM")); 
console.log(strToDate("Yesterday 3:28AM"));
console.log(strToDate("08/22/2011 3:28AM"));

Outputs:

Sun Sep 25 2011 03:28:00 GMT-0700 (Pacific Daylight Time)
Sat Sep 24 2011 03:28:00 GMT-0700 (Pacific Daylight Time)
Mon Aug 22 2011 03:28:00 GMT-0700 (Pacific Daylight Time)
鹿港小镇 2024-12-13 08:21:27

显然“今天”和“昨天”永远无法转换回真正的数字日期,目前看来您在这里要做的就是将其保存为“今天”和“昨天”,对吧?

您指定的 dd/mm/yyyy hh:mmxx 似乎始终以空格分隔。

所以你可以将字符串分成两部分,并将第一部分保存为日期。

JavaScript 函数:
http://www.w3schools.com/jsref/jsref_split.asp

至于如何从“今天”转换回26/09/2011等,您需要从XML方面寻求解决方案。

Obviously "Today" and "Yesterday" can never be transformed back to a real numeric date, for now it seems that what are you trying to do here is to save it as "Today" and "Yesterday", right?

It appears that the dd/mm/yyyy hh:mmxx you specified is always separated by a space.

so you can just split the string into two, and save the first part as your date.

the javascript function:
http://www.w3schools.com/jsref/jsref_split.asp

As for how to transform from "Today" back to 26/09/2011 etc, you need to seek solution from the XML side.

人生百味 2024-12-13 08:21:27

这是一个类似的问题:Javascript相当于php的strtotime()?

这里是链接的文章: http://w3schools.com/jS/js_obj_date.asp

以及建议的解决方案:

基本上,您可以使用日期构造函数来解析日期

var d=new Date("October 13, 1975 11:13:00");

Here is a similar question: Javascript equivalent of php's strtotime()?

Here is the linked article: http://w3schools.com/jS/js_obj_date.asp

And the suggested solution:

Basically, you can use the date constructor to parse a date

var d=new Date("October 13, 1975 11:13:00");
ぇ气 2024-12-13 08:21:27

有几种方法可以做到这一点。我将提供其中 2 个。

选项1:
如果日期始终位于字符串的开头,则可以使用 /([a-z0-9]*)\s|([0-9]{1,} 等正则表达式捕获第一部分)\/([0-9]{1,})\/([0-9]{1,})\s/ <- 我不是最好的正则表达式编写者。

选项2:
如果时间在一天之后立即到来,您也可以进行积极的展望(就像上面的示例一样。这里是 JS 正则表达式的正确语法的链接。http://www.javascriptkit.com/javatutors/redev2.shtml 您可以向下滚动到前瞻并查看一个示例,应该可以让您顺利离开那里。

There are a couple of ways you could do this. I will offer 2 of them.

option1:
If the day always at the beginning of the string you could capture the the first part by using a regular expression like /([a-z0-9]*)\s|([0-9]{1,})\/([0-9]{1,})\/([0-9]{1,})\s/ <- im not the best regex writer.

option2:
You could also do a positive look ahead if the time come immediately after the day (like your example above. Here is a link with the proper syntax for JS regex. http://www.javascriptkit.com/javatutors/redev2.shtml you can scroll down to lookaheads and see an example that should get you suared away there.

甜妞爱困 2024-12-13 08:21:27
var reTYD = /(today|yesterday|\d{1,2}\/\d{1,2}\/\d{4})/i;
console.log( myString.match(reTYD) );
var reTYD = /(today|yesterday|\d{1,2}\/\d{1,2}\/\d{4})/i;
console.log( myString.match(reTYD) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文