• Spring Boot + EasyUI Datebox和Datetimebox样例


            使用EasyUI的Datebox和Datetimebox组件,并对其进行适当的改造,比如更改日期格式、设置默认值或者将当前时间设置为默认值。

    一、运行结果

    二、实现代码

    1.代码框架

    2.实现代码

    SpringBootMainApplication.java:

    1. package com.xj.main;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.context.annotation.ComponentScan;
    5. /**
    6. * @Author: xjfu
    7. * @Create: 2023/10/20 7:33
    8. * @Description: SpringBoot启动类
    9. */
    10. @ComponentScan("com.xj")
    11. @SpringBootApplication
    12. public class SpringBootMainApplication {
    13. public static void main(String[] args) {
    14. try{
    15. SpringApplication.run(SpringBootMainApplication.class, args);
    16. }catch (Exception e){
    17. e.printStackTrace();
    18. }
    19. }
    20. }

    ThymeleafController.java

    1. package com.xj.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. /**
    5. * @Author: xjfu
    6. * @Create: 2023/10/20 7:42
    7. * @Description:
    8. */
    9. @RequestMapping("/easyui")
    10. @Controller
    11. public class ThymeleafController {
    12. //Datebox和Datetimebox案例
    13. @RequestMapping("/dateboxAndDatetimebox")
    14. public String dateboxAndDatetimebox(){
    15. //启动DateboxAndDatetimebox.html页面
    16. return "DateboxAndDatetimebox";
    17. }
    18. }

    DateboxAndDatetimebox.html

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Datetimebox 与 Dateboxtitle>
    6. <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
    7. <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
    8. <link rel="stylesheet" type="text/css" href="../demo.css">
    9. <script type="text/javascript" src="../../jquery.min.js">script>
    10. <script type="text/javascript" src="../../jquery.easyui.min.js">script>
    11. head>
    12. <body>
    13. <p>原装 Datetimebox: 默认日期格式为 MM/dd/yyyy HH:mm 默认时间为 3/4/2010 2:3p>
    14. <input class="easyui-datetimebox" name="birthday" data-options="required:true,showSeconds:false" value="3/4/2010 2:3" style="width:150px"><br>
    15. <p>改装 Datetimebox:1.日期格式更改为 yyyy-MM-dd HH:mm 2.默认值为当前时间p>
    16. <input class="easyui-datetimebox" name="birthday" data-options="formatter:myformatter2,parser:myparser2,required:true,showSeconds:false" value= "new Date()" style="width:150px"/><br>
    17. <p>原装 Datebox: 默认日期格式为 MM/dd/yyyy 无默认时间p>
    18. <input class="easyui-datebox"/><br>
    19. <p>改装 Datebox: 1.日期格式更改为 yyyy-MM-dd 2.默认时间为当前时间p>
    20. <input class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" value="newDate()"/><br>
    21. <script type="text/javascript">
    22. //格式化日期的函数:对日期进行格式化 yyyy-MM-dd
    23. function myformatter(date){
    24. var y = date.getFullYear();
    25. var m = date.getMonth()+1;
    26. var d = date.getDate();
    27. return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
    28. }
    29. //格式化日期的函数:对日期进行格式化 yyyy-MM-dd HH:mm
    30. function myformatter2(date){
    31. var y = date.getFullYear();
    32. var m = date.getMonth()+1;
    33. var d = date.getDate();
    34. var hour = date.getHours(); // 时
    35. var minutes = date.getMinutes(); // 分
    36. var seconds = date.getSeconds() //秒
    37. return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d) + ' ' + (hour<10?('0'+hour):hour) + ':' + (minutes<10?('0'+minutes):minutes);
    38. }
    39. //解析日期字符串的函数:该函数有一个 'date' 字符串,并返回一个日期值
    40. function myparser(s){
    41. if (!s) return new Date();
    42. var ss = (s.split('-'));
    43. var y = parseInt(ss[0],10);
    44. var m = parseInt(ss[1],10);
    45. var d = parseInt(ss[2],10);
    46. if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
    47. return new Date(y,m-1,d);
    48. } else {
    49. return new Date();
    50. }
    51. }
    52. //解析日期字符串的函数:该函数有一个 'date' 字符串,并返回一个日期值
    53. function myparser2(s){
    54. if (!s) return new Date();
    55. console.log('s==>' + s);
    56. var ss = (s.split(' '));
    57. console.log('ss==>' + ss);
    58. var s1 = ss[0].split('-');
    59. console.log('s1==>' + s1);
    60. var s2 = ss[1].split(':');
    61. console.log('s2==>' + s2);
    62. var y = parseInt(s1[0],10);
    63. var m = parseInt(s1[1],10);
    64. var d = parseInt(s1[2],10);
    65. var HH = parseInt(s2[0],10);
    66. var mm = parseInt(s2[1],10);
    67. if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
    68. return new Date(y,m-1,d,HH,mm);
    69. } else {
    70. return new Date();
    71. }
    72. }
    73. script>
    74. body>
    75. html>

    三、代码解析

    名称类型描述
    formatterfunction

    格式化日期的函数,该函数有一个 'date' 参数,并返回一个字符串值。下面的实例演示如何重写默认的格式化(formatter)函数。

    1. $.fn.datebox.defaults.formatter = function(date){
    2. var y = date.getFullYear();
    3. var m = date.getMonth()+1;
    4. var d = date.getDate();
    5. return m+'/'+d+'/'+y;
    6. }
    parserfunction

    解析日期字符串的函数,该函数有一个 'date' 字符串,并返回一个日期值。下面的实例演示如何重写默认的解析(parser)函数。

    1. $.fn.datebox.defaults.parser = function(s){
    2. var t = Date.parse(s);
    3. if (!isNaN(t)){
    4. return new Date(t);
    5. } else {
    6. return new Date();
    7. }
    8. }

    四、参考文献

    1.Easyui Datebox 日期框_EasyUI 插件

    2.EasyUI 日期格式(Date Format)_easyui demo

    3.JavaScript 获取当前日期时间 年月日 时分秒的方法_javascript技巧_脚本之家 

  • 相关阅读:
    VBA技术资料MF69:添加和删除工作表中的分页符
    AI人工智能大模型业务到底有多烧钱?
    Confluence 内容管理
    【BUG】第一次创建vue3+vite项目启动报错Error: Cannot find module ‘worker_threads‘
    考研数学笔记:一个例子让你明白什么是自由未知数什么是非自由未知数
    一起Talk Android吧(第四百零八回:间接绘制几何图形)
    有来实验室|第一篇:Seata1.5.2版本部署和开源商城订单支付业务实战
    多目标杜鹃搜索 (MOCS)优化算法(Matlab代码实现)
    16、window11+visual studio 2022+cuda+ffmpeg进行拉流和解码(RTX3050)
    OA系统都能为企业带来什么
  • 原文地址:https://blog.csdn.net/qq_21370419/article/details/134239337