加载中...

【uni-app】通过uni-app基础组件picker实现选择日期、时间的功能示例(完整代码+图文)

【uni-app】通过uni-app基础组件picker实现选择日期、时间的功能示例(完整代码+图文)

一、获取日期(基础)

New Image

  1. <template>
  2. <view class="container">
  3. <picker mode="date" @change="onDateChange" :value="date">
  4. <view class="date-picker">{{date}}</view>
  5. </picker>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. date: ''请选择日期''
  13. }
  14. },
  15. methods: {
  16. onDateChange: function(e) {
  17. this.date = e.detail.value;
  18. }
  19. }
  20. }
  21. </script>
  22. <style>
  23. .container {
  24. padding: 20rpx;
  25. background-color: #f5f5f5;
  26. }
  27. .date-picker {
  28. line-height: 80rpx;
  29. border: 1px solid #ccc;
  30. padding: 10rpx;
  31. text-align: center;
  32. background-color: #fff;
  33. }
  34. </style>

二、获取日期和时间(改进)

New Image

 

  1. <template>
  2. <view>
  3. <!--日期选择-->
  4. <view class="SelectDate">
  5. <view class="DateLabel">
  6. 预定日期
  7. </view>
  8. <view class="DateText">
  9. <picker mode="date" @change="onDateChange" :value="DateValue">
  10. <view class="date-picker">{{DateValue}}</view>
  11. </picker>
  12. </view>
  13. </view>
  14. <view class="SelectTime">
  15. <view class="TimeLabel">
  16. 预定时间
  17. </view>
  18. <view class="TimeText">
  19. <picker mode="time" @change="onTimeChange" :value="TimeValue">
  20. <view class="Time-picker">{{TimeValue}}</view>
  21. </picker>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. DateValue: "点击选择日期",
  31. TimeValue: "点击选择时间",
  32. }
  33. },
  34. methods: {
  35. onDateChange: function(e) {
  36. this.DateValue = e.detail.value;
  37. },
  38. onTimeChange: function(e) {
  39. this.TimeValue = e.detail.value;
  40. },
  41. }
  42. }
  43. </script>
  44. <style scoped>
  45. /* ## 日期 ## */
  46. .SelectDate {
  47. height: 40px;
  48. wdith: 100%;
  49. display: flex;
  50. flex-direction: grow;
  51. /* background-color: red; */
  52. }
  53. .DateLabel {
  54. width: 0;
  55. flex-grow: 3;
  56. background-color: #eaeaea;
  57. line-height: 40px;
  58. text-align: left;
  59. padding-left: 40px;
  60. border: 1px solid #f3f3f3;
  61. }
  62. .DateText {
  63. width: 0;
  64. flex-grow: 7;
  65. }
  66. .date-picker {
  67. background-color: aquamarine;
  68. height: 40px;
  69. line-height: 25px;
  70. width: 100%;
  71. border: 1px solid #f3f3f3;
  72. padding: 10rpx;
  73. text-align: center;
  74. background-color: #fff;
  75. color: #8f8f8f;
  76. }
  77. /* ## 时间 ## */
  78. .SelectTime {
  79. height: 40px;
  80. wdith: 100%;
  81. margin-top: 10px;
  82. display: flex;
  83. flex-direction: grow;
  84. /* background-color: red; */
  85. }
  86. .TimeLabel {
  87. width: 0;
  88. flex-grow: 3;
  89. background-color: #eaeaea;
  90. line-height: 40px;
  91. text-align: left;
  92. padding-left: 40px;
  93. border: 1px solid #f3f3f3;
  94. }
  95. .TimeText {
  96. width: 0;
  97. flex-grow: 7;
  98. }
  99. .Time-picker {
  100. background-color: aquamarine;
  101. height: 40px;
  102. line-height: 25px;
  103. width: 100%;
  104. border: 1px solid #f3f3f3;
  105. padding: 10rpx;
  106. text-align: center;
  107. background-color: #fff;
  108. color: #8f8f8f;
  109. }
  110. </style>