要将任何类型的日期转换为dd/mm/yyyy格式,您可以使用以下方法:
from datetime import datetime
def convert_date(date_string, input_format):
date_obj = datetime.strptime(date_string, input_format)
return date_obj.strftime("%d/%m/%Y")
date_string = "2022-01-01"
input_format = "%Y-%m-%d"
converted_date = convert_date(date_string, input_format)
print(converted_date)
function convertDate(dateString, inputFormat) {
const dateObj = new Date(dateString);
const day = dateObj.getDate().toString().padStart(2, "0");
const month = (dateObj.getMonth() + 1).toString().padStart(2, "0");
const year = dateObj.getFullYear();
return `${day}/${month}/${year}`;
}
const dateString = "2022-01-01";
const inputFormat = "YYYY-MM-DD";
const convertedDate = convertDate(dateString, inputFormat);
console.log(convertedDate);
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter {
public static void main(String[] args) {
String dateString = "2022-01-01";
String inputFormat = "yyyy-MM-dd";
String outputFormat = "dd/MM/yyyy";
try {
SimpleDateFormat inputFormatObj = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputFormatObj = new SimpleDateFormat(outputFormat);
Date dateObj = inputFormatObj.parse(dateString);
String convertedDate = outputFormatObj.format(dateObj);
System.out.println(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
这些代码示例将日期字符串从给定的输入格式转换为dd/mm/yyyy格式。请注意,这些示例仅适用于特定编程语言,因此请根据您的需求选择合适的示例。
领取专属 10元无门槛券
手把手带您无忧上云