简体   繁体   English

如何更改日期格式?

[英]How do I change the date format?

This is my js function to fetch data from database.这是我的 js function 从数据库中获取数据。 The data is being dynamically appended data to my modal popup from the js page.数据正在从 js 页面动态附加到我的模式弹出窗口中。 Currently the date format shown, is "2018-06-09 15:43:44"(as stored in database).当前显示的日期格式为“2018-06-09 15:43:44”(存储在数据库中)。 I wish to change the date format that is being displayed to dd-MMM-YYYY.我希望将显示的日期格式更改为 dd-MMM-YYYY。 Is there a way to achieve this?有没有办法做到这一点?

function fetch(url, id) {
    $.ajax({
        type: 'GET',
        url: url + id,
        headers: {
            'X-CSRF-TOKEN': $(`meta[name='_token']`).attr('content')
        },
        success: function (r) {
            let data = r;
//json value passed from controller in obj
            data = data.obj;
            let rows = '';
            $(data).each(function (index, row) {
                rows += `
                   <tr>
                            <td>${row.company_name}</td>
                            <td>${row.created_at}</td>
                   </tr>
            `;
            })
            $('.modalPage').html(rows);
        }
    });
}
use Carbon\Carbon;
...


$string = strtotime("2018-06-09 15:43:44") // row.created_at in your case;
$date = Carbon::createFromTimestamp($string);
return $date->format('d/m/Y'); // desired format. 

For Date formats check this https://www.php.net/manual/en/function.date.php对于日期格式,请检查此https://www.php.net/manual/en/function.date.php

code should be ideally in controller and pass this formatted date from there to view.理想情况下,代码应该在 controller 中,并从那里传递这个格式化的日期以查看。 Or better you can define accessor.或者更好的是你可以定义访问器。

https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor

Use the date.format library: :使用date.format库::

var dateFormat = require('dateformat');
var today= new Date(); 
var dateString = today.format("dd-mm-yyyy");

Convert in sql like this,像这样在 sql 中转换,

SELECT CONVERT(datetime, 105);

Look more format on this link此链接上查看更多格式

If you don't want to use laravel or any javascript package for date functions you can define your own functions like below.如果您不想将 laravel 或任何 javascript package 用于日期函数,您可以定义自己的函数,如下所示。

 let myDate = new Date("2020-08-11 20:59"); const formatNumbers = function(num) { return num < 10? "0" + num: num; } const formatDate = function (date){ let day = formatNumbers(date.getDate()); let month = formatNumbers(date.getMonth() + 1); let year = date.getFullYear() console.log(date) return day + '-' + month + '-' + year } document.getElementById('formattedDate').innerHTML = formatDate(myDate);
 <div> <span id="givenDate">Given Date: 2020-08-11 20:59</span> </div> <div> Formatted Date: <span id="formattedDate"></span> </div>

This code block is for your specific need if you want to create more generic formatter you can imrove your functions with following this documentation MDN Date如果您想创建更通用的格式化程序,此代码块可满足您的特定需求,您可以按照本文档MDN 日期改进您的功能

You can easily override the default time format from the model but if you don't want to do that you can also define your own function in Javascript and use that function to convert the format. You can easily override the default time format from the model but if you don't want to do that you can also define your own function in Javascript and use that function to convert the format.

You can use this你可以用这个

var my_date_format = function (input) {
   var d = new Date(Date.parse(input.replace(/-/g, "/")));
   var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 
   'Nov', 'Dec'];
   var date = d.getDay().toString() + " " + month[d.getMonth().toString()] + ", " + 
   d.getFullYear().toString();
   return (date);
}; 

and then call this function when the success method runs.然后在成功方法运行时调用此 function。 like this.像这样。

success: function (r) {
        let data = r;
//json value passed from controller in obj
        data = data.obj;
        let rows = '';
        $(data).each(function (index, row) {
            rows += `
               <tr>
                        <td>${row.company_name}</td>
                        <td>${my_date_format(row.created_at)}</td>
               </tr>
        `;
        })
        $('.modalPage').html(rows);
    }

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

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