js获取当前时间 格式为:2018-01-19 10:52:32

需求:

         js获取当前时间格式为 'yyyy-mm-dd hh-mm-ss';不会出现单数格式,如'2018-01-19 09:02:03',不会写成'2018-1-19 9:2:3'格式。


解决:

       

function current()  
{   
    var now = new Date();  
      
    var year = now.getFullYear();       //年  
    var month = now.getMonth() + 1;     //月  
    var day = now.getDate();            //日  
      
    var hh = now.getHours();            //时  
    var mm = now.getMinutes();          //分  
    var ss = now.getSeconds();           //秒  
      
    var clock = year + "-";  
      
    if(month < 10)  
        clock += "0";  
      
    clock += month + "-";  
      
    if(day < 10)  
        clock += "0";  
          
    clock += day + " ";  
      
    if(hh < 10)  
        clock += "0";  
          
    clock += hh + ":";  
    if (mm < 10) clock += '0';   
    clock += mm + ":";   
       
    if (ss < 10) clock += '0';   
    clock += ss;   
    return(clock);   
} 


Medicine medicine; cout << "请输入药品编号:"; cin >> medicine.id; cout << "请输入药品名称:可以使用JavaScript的Date对象获取当前时间,然后使用该对象提供的方法将当前时间转换成指定格式的"; cin >> medicine.name; cout << "请输入药品单价:"; cin >> medicine.price; cout << "请输入字符串。以下是将当前时间转换为2023-05-29 10:19:23格式的示例代码: ``销售数量:"; cin >> medicine.saleCount; medicine.saleAmount = medicine.price * medicine.saleCount; Medicine *temp` var now = new Date(); var year = now.getFullYear(); var month = ('0' + (now.getMonth() + 1)).slice = new Medicine[count + 1]; for (int i = 0; i < count; i++) { temp[i](-2); var day = ('0' + now.getDate()).slice(-2); var hour = ('0' + now.getHours()).slice(- = list[i]; } temp[count] = medicine; count++; delete[] list; list = temp; } // 删除2); var minute = ('0' + now.getMinutes()).slice(-2); var second = ('0' + now.getSeconds()).slice(-2药品信息 void deleteMedicine(Medicine *&list, int &count) { string id; cout << "请输入要删除); var datetime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; console.log的药品编号:"; cin >> id; int index = searchById(list, count, id); if (index == -(datetime); ``` 其中,now.getFullYear()方法可以获取当前年份,now.getMonth()方法可以获取当前月份(注意,返回1) { cout << "未找到药品编号为 " << id << " 的药品" << endl; return值是0-11,需要加1),now.getDate()方法可以获取当前日期,now.getHours()方法可以获取当前小时数,; } Medicine *temp = new Medicine[count - 1]; for (int i = 0; i < index; inow.getMinutes()方法可以获取当前分钟数,now.getSeconds()方法可以获取当前秒数。需要注意的是,以上方法返回的++) { temp[i] = list[i]; } for (int i = index; i < count - 1; i值都是数字,如果需要将其转换为字符串并进行格式化,需要使用一些字符串处理方法,如上面示例代码中使用的slice()方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值