【问题标题】:Python function to format date用于格式化日期的Python函数
【发布时间】:2022-12-06 02:29:28
【问题描述】:

实现函数 date_format,它接受日期列表作为字符串,并返回一个新的字符串列表,以 YYYYMMDD 格式表示日期。所有传入日期都是有效日期,但返回列表中应包含以下格式之一的日期:YYYY/MM/DD、DD/MM/YYYY 和 MM-DD-YYYY。 这里 YYYY、MM、DD 是代表年、月、日的数字。

例如:date_format(["2010/01/20","19/12/2014","01-16-2005","20140525"]) 应该返回列表 ["20100120","19122014","01162005"]

请帮帮我

【问题讨论】:

  • 在问题中将您自己的努力(代码)显示为格式正确的文本。
  • 对不起,我对此很陌生,正在努力尝试概念,如果可以的话,请帮助我
  • 我认为你正在寻找某人为你做这件事而你甚至没有检查正则表达式的基础知识。再次检查您的预期结果,如果您只收集数字(或删除数字以外的所有内容),您会得到您想要的结果。
  • 您可以使用 regex101.com 来玩正则表达式(将 flavor 设置为 Python)。

标签: python regex string date


【解决方案1】:

这是一种可能的实现。

  def date_format(dates):
  result = []
  for date in dates:
    # Split the date string into its component parts
    parts = date.split('/')
    if len(parts) == 3:
      year, month, day = parts
    else:
      parts = date.split('-')
      year, month, day = parts
    
    # Re-arrange the date parts in the desired format
    formatted_date = year + month + day
    result.append(formatted_date)
  return result

【讨论】:

  • 不要不加解释就替别人做功课
猜你喜欢
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多