gpt4 book ai didi

javascript - 在javascript中将字符串 'mm/dd/yyyy'转换为 'day of week, month name, year'

转载 作者:行者123 更新时间:2023-11-28 16:24:19 26 4
gpt4 key购买 nike

我按照以下格式从 Facebook 获取 Facebook 用户 friend 的生日:

10/07/1967 or just the day and month as 10/07

我想将其显示为“October, 07, 1967”或“October, 07”

有没有办法将此字符串转换为日期并在 Javascript 中对其进行格式化?

最佳答案

var MONTHS = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var myDate, myFormatDate;
var date_str ='10/07/1967';
var t = date_str.split("/");
if(t[2]) {
myDate = new Date(t[2], t[0] - 1, t[1]);
myFormatDate = MONTHS[myDate.getMonth()] + "," + myDate.getDate() + "," + myDate.getFullYear();
} else {
myDate = new Date(new Date().getFullYear(), t[0] - 1, t[1]);
myFormatDate = MONTHS[myDate.getMonth()] + "," + mydate.getDate();
}

= 结果:
= myDate -- 日期对象
= myFormatDate -- 格式化日期字符串“October, 07, 1967”

关于javascript - 在javascript中将字符串 'mm/dd/yyyy'转换为 'day of week, month name, year',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8533357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com