简体   繁体   English

如何将字符串日期转换为更易读的版本

[英]How to Convert a string date to a more readable version

I have a date in my database like this 2021/04/26我的数据库中有这样的日期2021/04/26

And I wanna convert this to string date to: Monday, April 04我想将其转换为字符串日期:星期一,4 月 4 日

I have tried In many different ways.我已经尝试了许多不同的方式。 I am very week in working with dates in JavaScript.我在 JavaScript 中处理日期非常周。

I tried to do like this new Date('2021/04/26').getDate() // Which return very strange date.我试图这样做new Date('2021/04/26').getDate() // Which return very strange date.

Can Anyone help me to convert that string to this Monday, April 04谁能帮我把那个字符串转换成这个星期一,4 月 4 日

Method 1: By using only the build in functions this is the closest you can get to your example:方法 1:通过仅使用内置函数,这是您可以获得的最接近示例的方法:

new Date('2021/04/26').toDateString()

Method 2: Do it on your own like this:方法2:像这样自己做:

  const months = {
  0: 'January',
  1: 'February',
  2: 'March',
  3: 'April',
  4: 'May',
  5: 'June',
  6: 'July',
  7: 'August',
  8: 'September',
  9: 'October',
  10: 'November',
  11: 'December'
}

const days = [
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday'
]

let yourDate = new Date('2021/04/26');
let yourDateString = days[yourDate.getDay()]+', '+months[yourDate.getMonth()]+' '+yourDate.getFullYear();

RESULT: "Monday, April 2021"结果:“2021 年 4 月,星期一”

Method 3: Use a third party library.方法三:使用第三方库。

If all you need is to output the date in that format, and not do any date based logic, you could convert the date when selecting from the database (you can of course still select the original date column at the same time).如果您只需要 output 该格式的日期,并且不执行任何基于日期的逻辑,则可以在从数据库中选择时转换日期(当然您仍然可以同时 select 原始日期列)。 This depends on using the correct date type for the column.这取决于对列使用正确的日期类型。

For MySQL: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format对于 MySQL: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

SELECT date_format( DATE_COLUMN, '%W, %M %d' ) as NEW_DATE, ...

You could also convert it using a server side language like PHP if you are using one.如果您使用的是服务器端语言,您也可以使用 PHP 等服务器端语言对其进行转换。 If you let us know how you are fetching and outputting the data there might be a better way.如果您让我们知道您是如何获取和输出数据的,那么可能会有更好的方法。

I will leave the parsing of the date into a Date object out of this answer.我会将日期的解析保留为 Date object 这个答案。 Just passing the string as-is might work, or it might break on some setups/locales.只是按原样传递字符串可能会起作用,或者它可能会在某些设置/语言环境中中断。 You are usually better off manually parsing it and explicitly setting year, month and date.您通常最好手动解析它并明确设置年、月和日期。

You can print the date in a more readable format using Date.toLocaleDateString ( mdn ).您可以使用Date.toLocaleDateString ( mdn ) 以更易读的格式打印日期。 This does not allow you to get your date in any format you can possibly desire, but it does a reasonable job in getting your date in a readable format for the language you want to display it in.这不允许您以您可能想要的任何格式获取日期,但它可以合理地以您想要显示的语言的可读格式获取日期。

You can get the options from the implementation of the localisation helper .您可以从本地化助手的实现中获得选项。

 const currentDate = new Date(); console.log(currentDate.toLocaleDateString('en-gb', { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' })); console.log(currentDate.toLocaleDateString('en-gb', { weekday: 'narrow', year: '2-digit', month: 'long', day: '2-digit' }));

If you want full control over what you output, you will need a library.如果您想完全控制 output,您将需要一个库。 Selecting that library is out-of-scope for Stackoverflow though, so I will not suggest one here.不过,选择该库超出了 Stackoverflow 的范围,所以我不会在这里推荐一个。

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

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