简体   繁体   English

如何从时间戳中获取日期?

[英]How to get date from a timestamp?

I have 2 timestamps with different format:我有 2 个不同格式的时间戳:

1/2/2021 21:15
19-3-2021 21:15

Is there a method in javascript to get just the date for these timestamps? javascript 中是否有一种方法可以仅获取这些时间戳的日期?

Expected output:预期 output:

'1/2/2021'
'19/3/2021'

I know using substr() is not effective as the length of the date string can vary.我知道使用substr()无效,因为日期字符串的长度可能会有所不同。

Assuming the two timestamp formats are the only ones to which you need to cater, we can try:假设这两种时间戳格式是您需要满足的唯一格式,我们可以尝试:

 function getDate(input) { return input.replace(/\s+\d{1,2}:\d{1,2}$/, "").replace(/-/g, "/"); } console.log(getDate("1/2/2021 21:15")); console.log(getDate("19-3-2021 21:15"));

The first regex replacement strips off the trailing time component, and the second replacement replaces dash with forward slash.第一个正则表达式替换去掉了尾随时间部分,第二个替换用正斜杠替换了破折号。

  1. Use split() to convert string into array based on space.使用split()将字符串转换为基于空间的数组。

  2. Then use replaceAll() on each element of the array, which will replace all the dash(-) which are in dates to slash (/)然后在数组的每个元素上使用replaceAll() ,它将替换日期中的所有破折号(-) 到斜杠 (/)

  3. Use includes() to check if slash(/) is present or not, as it will separate data(d/m/y) with time(h:m) 使用 includes()检查斜线(/) 是否存在,因为它将数据(d/m/y) 与时间(h:m) 分开

 function getDateFun(timestamp) { let timeStr = timestamp; let splitStamp = timeStr.split(" "); let dates = []; for (let i = 0; i < splitStamp.length; i++) { if (splitStamp[i].includes("/") || splitStamp[i].includes("-")) dates.push(splitStamp[i]); } console.log(dates.toString()); } getDateFun("1/2/2021 21:15"); getDateFun("19-3-2021 21:15"); getDateFun("1/2/2021 21:15 19-3-2021 21:15");

Update更新

Based on RobG comment, the same can be achieved by using Regular Expressions and replace() method.根据RobG注释,同样可以通过使用正则表达式replace()方法来实现。

 function getDateFun(timestamp){ return timestamp.split(' ')[0].replace(/\D/g, '/'); } console.log(getDateFun("28/03/2021 07:50")); console.log(getDateFun("19-02-2021 15:30"));

 function getDateFun(timestamp){ return timestamp.replace(/(\d+)\D(\d+)\D(\d+).*/,'$1/$2/$3') } console.log(getDateFun("28/03/2021 07:50")); console.log(getDateFun("19-02-2021 15:30"));

Assuming that your dates have a space character between the date and time you can use the split() method:假设您的日期在日期和时间之间有一个空格字符,您可以使用split()方法:

 let firstDate = '1/2/2021 21:15'; let secondDate = '19-3-2021 21:15'; // [0] is the first element of the splitted string console.log(firstDate.split(" ")[0]); console.log(secondDate.split(" ")[0]);

Or you can then also use substr() by first finding the position of the space character:或者您也可以通过首先找到空格字符的 position 来使用substr()

 let firstDate = '1/2/2021 21:15'; let secondDate = '19-3-2021 21:15'; let index1 = firstDate.indexOf(' '); let index2 = secondDate.indexOf(' '); console.log(firstDate.substr(0, index1)); console.log(secondDate.substr(0, index2));

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

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