Some uses and methods of javascript (date) Date object

1. Get the specific time from the time and date object

// 从时间日期对象中获取具体的时间
var date = new Date()
console.log(date);
// 获取年份 - 对象.getFullYear()
var year = date.getFullYear()
console.log(year);
// 获取月份 - 对象.getMonth() - 在对象中,使用0~11来描述1~12月
var month = date.getMonth() + 1 // 因为获取到的月份会比实际月份小1,所以通常会+1
console.log(month);
// 获取日 - 对象.getDate()
var d = date.getDate() // 注意:千万不要使用date变量表示日
console.log(d);
// 获取星期 - 对象.getDay()
var day = date.getDay()
console.log(day);
// 获取时 - 对象.getHours()
var hour = date.getHours()
console.log(hour);
// 获取分 - 对象.getMinutes()
var minute = date.getMinutes()
console.log(minute);
// 获取秒 - 对象.getSeconds()
var second = date.getSeconds()
console.log(second);
// 获取毫秒 - 对象.getMilliseconds() - 1s === 1000ms
var mill = date.getMilliseconds()
console.log(mill);

2. Set date and time

 Set specific time

        setFullYear()

        setMonth() - argument is 1 less than the actual month

        setDate()

        setHours()

        setMinutes()

        setSeconds()

        setMilliseconds()

        setTime()

    Note: In setting the time, the day of the week is not set because the day of the week is generated based on the year, month and day and cannot be set.

example:

// 通过时间日期对象设置时间
var date = new Date() // 获取到当前时间对象
console.log(date);
// 将这个对象变成另外一个时间的对象 - 设置这个时间
// 设置年份:对象.setFullYear(目标年份)
date.setFullYear(2023)
console.log(date);
// 设置月份:对象.setMonth(目标月份) - 0~11来描述1~12月
date.setMonth(4) // 参数是0~11,参数:比实际月份-1
console.log(date);
// 设置日:对象.setDate(目标日)
date.setDate(25)
console.log(date);
// 设置时:对象.setHours(目标时)
date.setHours(15)
console.log(date);
// 设置分:对象.setMinutes(目标分)
date.setMinutes(50)
console.log(date);
// 设置秒:对象.setSeconds(目标秒)
date.setSeconds(59)
console.log(date);
// 设置毫秒:对象.setMilliseconds(目标毫秒数)
date.setMilliseconds(500)
console.log(date);
// 通过设置具体的时间,时间日期对象就发生了变化 - 通过设置以后的对象获取具体的时间
console.log( date.getMilliseconds() );

// 设置时间戳:对象.setTime(时间戳) - 将当前这个对象的时间切换成指定时间戳的对象
date.setTime(0)
console.log(date);

Related cases:

What day was it 100 days ago?

Idea:

Idea analysis: Before 100, we still have to work backwards to 100 days based on the current time.

 Need the current time and date object - use the current time - 100 days - use the current timestamp - the number of milliseconds in 100 days - the timestamp of the time 100 days ago

 Set a time object based on a timestamp 100 days ago - get a time object 100 days ago

 Get month and day

 var date = new Date()
    console.log(date)
    // 2.获取当前时间戳
    var time = date.getTime()
    // console.log(time)
    // 3.用当前的时间戳-100天的时间戳
    var time1 = 100 * 24 * 3600 * 1000 //得到100天的时间戳
    var time2 = time - time1 //得到的是1970年到一百天前的时间戳
    // console.log(time2)
    // 根据100天前的时间戳设置时间
    date.setTime(time2)
    // 获取月份和日期
    var mouth = date.getMonth() + 1;
    var day = date.getDate()
    console.log('100天前是' + mouth + '月' + day + '日')

3. Create a date object at a specified time

  String parameters:

    var date = new Date('2022-10-1 08:59:03')

    console.log(date);

    Numeric parameters: Note - the month is 1 less than the actual month

     var date = new Date(2022,9,1,8,59,3)

     console.log(date);

    Timestamp parameters:

    var date = new Date()

    console.log(date);

    

    Summarize:

        new Date() - Gets the current time without parameters

        new Date('y year m month d day h:i:s')

        new Date(y,m,d,h,m,s)

        new Date(time)

    

Case: How many days are left until the National Day?

Idea: National Day timestamp - current timestamp = millisecond difference

Convert to days based on millisecond difference

  // 1、获取国庆的时间日期对象
    var guoqing = new Date(2022, 10, 1)
    // console.log(guoqing)
    // 2、获取国庆的时间戳
    guoqing = guoqing.getTime()
    // console.log(guoqing)
    // 3.获取当前时间对象
    var now = new Date()
    // console.log(now)
    // 4、获取当前的时间戳
    now = now.getTime()
    // console.log(now)
    // 5.求两者的毫秒差
    var diff = guoqing - now;
    // console.log(diff)
    // 6.将毫秒换算为天数
    var day = parseInt(diff / 1000 / 60 / 60 / 24)
    console.log('距离国庆还有' + day + '天');

4. How to obtain timestamp

1. .Time and date object.getTime()

 var date= new Date()

 var time=date.getTime()

  console.log(time)

2. Put + in front of new:

    // var date = +new Date()

    // console.log(date);

Small addition: Adding a + sign in front of a string can convert characters into numbers.

3. Use the constructor Date: Date.parse('year-month-day hour: minute: second')

var guoqing=Date.parse('2022-10-1')

console.log(guoqing)

5. Formatting time

Under normal circumstances, when we output time and date objects, they look particularly ugly.

// console.log( new Date() );

 So here are three methods for formatting time:

1. Format year, month and day

   var date = new Date()

    var str1 = date.toLocaleDateString()

    console.log(str1);

2. Formatting hours, minutes and seconds

    var date = new Date()

    var str2 = date.toLocaleTimeString()

    console.log(str2);

3. Write the first two together and format the year, month, day, hour, minute and second.

 var date = new Date()

    //Convert to a more convenient time display - format the output time

    //Format the entire year, month, day, hour, minute and second

    var str = date.toLocaleString()

    console.log(str);

6. Comprehensive demo

1. Define a function that requires two time nodes to be passed in and can return the number of days, hours, minutes, and seconds between the two time nodes.

function timeDiff(time1, time2) {
    var now = +new Date(time1)
    var guoqing = +new Date(time2)
    var diff = Math.abs(guoqing - now)
    console.log(diff);
    var day = parseInt(diff / 1000 / 60 / 60 / 24)
    var hour = parseInt(diff / 1000 / 60 / 60) % 24
    var minute = parseInt(diff / 1000 / 60) % 60
    var second = parseInt(diff / 1000) % 60
    // console.log('相差'+day+'天'+hour+'小时'+minute+'分钟'+second+'秒');

    return '相差'+day+'天'+hour+'小时'+minute+'分钟'+second+'秒'
}

var str = timeDiff('2022-03-18 14:10:00', '2022-10-01 00:00:00')
console.log(str);

2. Write a function to format the current time as "YYYY-MM-DD HH:ii:ss"

function formateDate(date) {
    // var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var d = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    // console.log(year, month, d, hour, minute, second);
    // console.log(year + "-" + month + '-' + d + ' ' + hour + ':' + minute + ':' + second);

    // 月/日/时/分/秒 如果他们是一位数字,给他补0
    // month = month < 10 ? '0' + month : month
    // d = d < 10 ? '0' + d : d
    // hour = hour < 10 ? '0' + hour : hour
    // minute = minute < 10 ? '0' + minute : minute
    // second = second < 10 ? '0' + second : second

    month = bu0(month)
    d = bu0(d)
    hour = bu0(hour)
    minute = bu0(minute)
    second = bu0(second)


    return year + "-" + month + '-' + d + ' ' + hour + ':' + minute + ':' + second
}

function bu0(num) {
    num = num < 10 ? '0' + num : num
    return num
}

var str = formateDate(new Date())
console.log(str);

Guess you like

Origin blog.csdn.net/weixin_45441470/article/details/123644633