官术网_书友最值得收藏!

  • Vue.js 2.x by Example
  • Mike Street
  • 194字
  • 2021-07-02 20:00:27

Format registered date

The registered date field in the data is computer friendly, which is not very human-friendly to read. Create a new method titled formatDate that takes one parameter — similar to the formatBalance method previously.

If you want full customization of the display of your date, there are several libraries available, such as moment.js, that give you much more flexibility over the output of any date and time-based data. For this method, we are going to use a native JavaScript function, to LocaleString():

      formatDate(date) {
let registered = new Date(date);
return registered.toLocaleString('en-US');
}

With the registered date, we pass it to the native Date() function so JavaScript knows to interpret the string as a date. Once stored in the registered variable, we return the object as a string with the toLocaleString() function. This function accepts a huge array of options (as outlined on MDN) to customize the output of your date. For now, we'll pass it the locale we wish to display and use the defaults for that location. We can now utilize our method in the view:

      <td>{{ formatDate(person.registered) }}</td>

Each table row should now look like the following:

主站蜘蛛池模板: 邵阳县| 柳河县| 长沙市| 博野县| 崇明县| 准格尔旗| 盐山县| 泗水县| 青海省| 陈巴尔虎旗| 绥化市| 定西市| 明光市| 台南县| 古丈县| 佳木斯市| 营口市| 石首市| 台中市| 武义县| 大埔县| 图片| 永春县| 宁武县| 五华县| 中西区| 上杭县| 瑞丽市| 光泽县| 南木林县| 壶关县| 斗六市| 包头市| 南丰县| 万荣县| 鸡东县| 姜堰市| 蒲城县| 齐河县| 阳曲县| 遵义县|