封装 - 日期的获取以及格式转换

不念不忘少年蓝@ 2021-09-30 15:00 438阅读 0赞

1、获取当天所在的年份

  1. function getYear () {
  2. let mydate = new Date();
  3. let value = mydate.getFullYear();
  4. return value;
  5. }

2、获取当天所在的月数

  1. function getMonth () {
  2. let mydate = new Date();
  3. let value = mydate.getMonth() + 1;
  4. return value;
  5. }

3、获取当天所在的号数

  1. function getDate () {
  2. let mydate = new Date();
  3. let value = mydate.getDate();
  4. return value;
  5. }

4、获取天数

  1. function getDates(year, month) {
  2. let dates = new Date(year, month, 0).getDate();
  3. return dates;
  4. }

5、日期格式转化1    2018,1->2018/01

  1. function formatDating(year, month, date) {
  2. month = fn.addZero(month);
  3. if (date) {
  4. date = fn.addZero(date);
  5. }
  6. return year + '/' + month + (date ? '/' + date : '');
  7. }

6、日期格式转化2    2018,1->2018年01

  1. function formatDating2(year, month, date) {
  2. let mon = month.toString();
  3. if (mon.length < 2 && month < 10) {
  4. month = fn.addZero(month);
  5. }
  6. if (date) {
  7. let dat = date.toString();
  8. if (dat.length < 2 && date < 10) {
  9. date = fn.addZero(date);
  10. }
  11. }
  12. return year + '年' + month + '月' + (date ? date + '日' : '');
  13. }

7、年份

  1. function getyears() {
  2. let arr = [];
  3. for (var i = 2000; i < 2030; i++) {
  4. arr.push({
  5. name: i,
  6. value: i
  7. })
  8. }
  9. return arr;
  10. }

8、月份

  1. function getmonths() {
  2. let arr = [];
  3. for (var i = 1; i <= 12; i++) {
  4. arr.push({
  5. name: i,
  6. value: i
  7. })
  8. }
  9. return arr;
  10. }

9、日期格式定向转化 2018/01->2018年01月,2018/01/01->2018年01月01日

  1. function cToe(str, type, ind) {
  2. let restr = ''
  3. str = str.toString();
  4. if (ind) {
  5. type = type == -1 || type == 2 ? type : type + 1
  6. }
  7. if (type == 0 || type == 3) {
  8. //日+自定义 2018/01/01
  9. restr = str.substring(0, 4) + '/' + str.substring(5, 7) + '/' + str.substring(8, 10) + '';
  10. }
  11. else if (type == 1) {
  12. //月 2018/01
  13. restr = str.substring(0, 4) + '/' + str.substring(5, 7) + '';
  14. }
  15. else if (type == 2) {
  16. //季度 2018 第一季度
  17. restr = str.substring(0, 4) + ' ' + str.substring(5, str.length);
  18. }
  19. else if (type == -1) {
  20. //年份
  21. str = str.toString();
  22. restr = str.substring(0, str.length);
  23. }
  24. return restr;
  25. }

10、处理年月的格式

  1. function tochinesedate(params, type) {
  2. let arr = params.split(type);
  3. let time = arr[0] + '年' + arr[1] + '月';
  4. return time;
  5. }

发表评论

表情:
评论列表 (有 0 条评论,438人围观)

还没有评论,来说两句吧...

相关阅读

    相关 日期格式转换

    日期格式转换   在java或js中 我们常常会进行日期格式的转换  然而每次都去写很麻烦 所以在这里 我整理了一个dateformat的工具类 希望对大家有用!![