指示 JavaScript 的日期读取日期字符串 dd/mm/yyyy 而不是 mm/dd/yyyy

发布于 2025-01-19 15:01:45 字数 401 浏览 0 评论 0原文

我想解析日期字符串01/04/2022 4月1日 not bike 1月4日 by JavaScript Date()。

有什么方法我可以强制/指示JavaScript的date()将日期字符串读取为dd/mm/yyyy,而不是mm/dd/yyyy < /代码>?

new Date('01/04/2022') // returns Tue Jan 04 2022 00:00:00 GMT+0100 (Central European Standard Time)

谢谢

I would like to parse the date string 01/04/2022 as April 1st and not like January 4th by JavaScript Date().

Is there any way I can force/instruct javascript's Date() to read a date string as dd/mm/yyyy and not as mm/dd/yyyy?

new Date('01/04/2022') // returns Tue Jan 04 2022 00:00:00 GMT+0100 (Central European Standard Time)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱人配狗天长地久 2025-01-26 15:01:45

创建一个函数来完成这项工作是非常微不足道的

const dmy = s => new Date(...s.split(/\D+/).reverse().map((v,i)=>+v-(i%2)))
console.log(dmy('01/04/2022').toString())

您可以将其添加到date ...所以您总是知道在哪里可以找到它

Date.dmy = s => new Date(...s.split(/\D+/).reverse().map((v,i)=>+v-(i%2)))
console.log(Date.dmy('01/04/2022').toString())
console.log(Date.dmy('01/13/2022').toString()) // oops, didn't fail

但是,以上不会在无效的日期失败

,这样写

Date.dmy = s => {
    const a = s.split('/');
    return new Date(`${a[1]}/${a[0]}/${a[2]}`);
}
console.log(Date.dmy('01/04/2022').toString())
console.log(Date.dmy('01/13/2022').toString())

It's quite trivial to create a function to do the job

const dmy = s => new Date(...s.split(/\D+/).reverse().map((v,i)=>+v-(i%2)))
console.log(dmy('01/04/2022').toString())

You could add it to Date ... so you always know where to find it

Date.dmy = s => new Date(...s.split(/\D+/).reverse().map((v,i)=>+v-(i%2)))
console.log(Date.dmy('01/04/2022').toString())
console.log(Date.dmy('01/13/2022').toString()) // oops, didn't fail

however, the above won't fail on an invalid date

To do so, write it like this

Date.dmy = s => {
    const a = s.split('/');
    return new Date(`${a[1]}/${a[0]}/${a[2]}`);
}
console.log(Date.dmy('01/04/2022').toString())
console.log(Date.dmy('01/13/2022').toString())

故人的歌 2025-01-26 15:01:45

您可以使用法国格式:

const start = Date.now();
const date = new Date(start);
const formatted = date.toLocaleDateString("fr-FR")

You could use the french formating :

const start = Date.now();
const date = new Date(start);
const formatted = date.toLocaleDateString("fr-FR")
梦回旧景 2025-01-26 15:01:45

解决方案可能是一个字符串,希望下面的代码有帮助:

let day = new Date().getDate()
let months = new Date().getMonth();
let year = new Date().getFullYear();

const formatDate = (day, months, year) =>{
    let total = ""
    let error = false
    let errorMessage = ""
    if(day != null && months != null && year != null){
        error = false
        if(months <= 9){
            total = `0${day}/0${months}/${year}`
        }else{
            total = `0${day}/${months}/${year}`;
        }
    }else{
        error = true
        errorMessage = "ERROR, in day, month, or year"
        return errorMessage
    }
    return total
}

// Calling Function
console.log(formatDate(day, months, year))

The solution may be a string hope the below code helps:

let day = new Date().getDate()
let months = new Date().getMonth();
let year = new Date().getFullYear();

const formatDate = (day, months, year) =>{
    let total = ""
    let error = false
    let errorMessage = ""
    if(day != null && months != null && year != null){
        error = false
        if(months <= 9){
            total = `0${day}/0${months}/${year}`
        }else{
            total = `0${day}/${months}/${year}`;
        }
    }else{
        error = true
        errorMessage = "ERROR, in day, month, or year"
        return errorMessage
    }
    return total
}

// Calling Function
console.log(formatDate(day, months, year))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文