JavaScript中new Date()参数问题

JavaScript下,new Date([params]),参数传递有以下五种方式:

  • ①:new Date("month dd, yyyy hh:mm:ss");
  • ②:new Date("month dd, yyyy");
  • ③:new Date(yyyy, month, dd, hh, mm, ss);
  • ④:new Date(ms)

ms:时间戳,是 创建的时间 和 GMT时间1970年1月1日之间相差的毫秒数。获取时间戳方式:new Date().getTime();

new Date([params])的参数的取值范围:

  • month:用整数表示月份,从0(1月)到11(12月)
  • dd:表示一个 月中的第几天,从1到31
  • yyyy:四位数表示的年份
  • hh:小时数,从 0 到 23 的整数
  • mm:分钟数,从 0 到 59 的整数
  • ss:秒数,从0到59的整数
  • ms:毫秒数,为大于等于0的整数

判断指定字符串是否是一个 URL

判断指定字符串是否是一个 URL。这段代码定义了一个 isUrl() 方法,方法内部使用正则表达式来验证参数字符串是否是符合 URL 规则;同时,还演示了如何测试我们定义的 isUrl() 方法。

  1. // 检测是否是合法的 URL
  2. function isUrl(url) {
  3. const regex = /\b(https?):\/\/[\-A-Za-z0-9+&@#\/%?=~_|!:,.;]*[\-A-Za-z0-9+&@#\/%=~_|]/i;
  4. return regex.test(url);
  5. }
  6. // 测试代码
  7. let url = "https://www.dute.org";
  8. let r = isUrl(url);
  9. // 输出结果
  10. console.log(url);
  11. console.log(r);

TypeScript中生产uuid的四种方式:

  • 方式一: ```typescript function generateUUID() { return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(/[xy]/g, function (c) {
    1. const r = Math.random() * 16 | 0,
    2. v = c == 'x' ? r : (r & 0x3 | 0x8);
    3. return v.toString(16);
    }); }

// “a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7” const uuid = generateUUID();

  1. - 方式二:
  2. ```typescript
  3. function generateUUID() {
  4. const s = [];
  5. const hexDigits = "0123456789abcdef";
  6. for (let i = 0; i < 36; i++) {
  7. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  8. }
  9. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  10. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  11. s[8] = s[13] = s[18] = s[23] = "-";
  12. return s.join("");
  13. }
  14. // "a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7"
  15. const uuid = generateUUID();
  • 方式三: ```typescript function generateUUID() { function gen() {
    1. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    } return (gen() + gen() + “-“ + gen() + “-“ + gen() + “-“ + gen() + “-“ + gen() + gen() + gen()); }

// “a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7” const uuid = generateUUID();

  1. - 方式四:
  2. ```typescript
  3. // 指定长度和基数
  4. function generateUUID(len, radix) {
  5. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  6. let uuid = [], i;
  7. radix = radix || chars.length;
  8. if (len) {
  9. // Compact form
  10. for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  11. } else {
  12. // rfc4122, version 4 form
  13. let r;
  14. // rfc4122 requires these characters
  15. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  16. uuid[14] = '4';
  17. // Fill in random data. At i==19 set the high bits of clock sequence as
  18. // per rfc4122, sec. 4.1.5
  19. for (i = 0; i < 36; i++) {
  20. if (!uuid[i]) {
  21. r = 0 | Math.random() * 16;
  22. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  23. }
  24. }
  25. }
  26. return uuid.join('');
  27. }
  28. // "277571702EE33E11"
  29. generateUUID(16, 16)
  • 格式化时间 ``javascript /*将时间`部分设置为: yyyy-MM-dd 00:00:00*/ function formatDate_beginOfDay(timestamp) { var date = new Date(timestamp); return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0).getTime(); }

/时间部分设置为: yyyy-MM-dd 23:59:59/ function formatDate_endOfDay(timestamp) { var date = new Date(timestamp); return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59).getTime(); }

  1. - 获取每个月的最后一天
  2. ```javascript
  3. /*
  4. 由于JavaScript中day的范围为1~31中的值,所以当设为0时,会向前 一天,也即表示上个月的最后一天。
  5. 所以: new Date(2017,2,0).getDate() 会获取到`2`月份的最后一天
  6. */
  7. function mGetDate(year, month){
  8. var d = new Date(year, month, 0);
  9. return d.getDate();
  10. }
  11. var totalDay = mGetDate(2004,9); //30
  12. console.log(totalDay)

去除字符串两边的空格

  1. var str:string = "\n\n\t\t dfd \n\n\t\t";
  2. str = str.replace(/(^\s*)|(\s*$)/g, "")
  3. // 输出对象
  4. console.log(str);
  5. console.log(str.length);

电表-遥测数据字段筛选规则

  1. // 1.接收到上传的遥测数据
  2. var inMsg = {
  3. import_active_energy: "import_active_energy",
  4. voltage_a: "voltage_a",
  5. voltage_b: "voltage_b",
  6. voltage_c: "voltage_c",
  7. current_a: "current_a",
  8. current_b: "current_b",
  9. current_c: "current_c",
  10. cycle_delta_usage: "cycle_delta_usage",
  11. day_delta_usage: "day_delta_usage",
  12. month_delta_usage: "month_delta_usage",
  13. };
  14. // 2.创建遥测数据的模板
  15. var outMsg = {
  16. filterField: function () { }
  17. };
  18. // 3.编写遥测数据的字段筛选规则
  19. outMsg.filterField = function () {
  20. // 判断某个`key`是否存在。true:则入库;false:则移除
  21. if (inMsg.hasOwnProperty("import_active_energy")) {
  22. outMsg["import_active_energy"] = inMsg.import_active_energy;
  23. }
  24. if (inMsg.hasOwnProperty("voltage_a")) {
  25. outMsg["voltage_a"] = inMsg.voltage_a;
  26. }
  27. if (inMsg.hasOwnProperty("voltage_b")) {
  28. outMsg["voltage_b"] = inMsg.voltage_b;
  29. }
  30. if (inMsg.hasOwnProperty("voltage_c")) {
  31. outMsg["voltage_c"] = inMsg.voltage_c;
  32. }
  33. if (inMsg.hasOwnProperty("current_a")) {
  34. outMsg["current_a"] = inMsg.current_a;
  35. }
  36. if (inMsg.hasOwnProperty("current_b")) {
  37. outMsg["current_b"] = inMsg.current_b;
  38. }
  39. if (inMsg.hasOwnProperty("current_c")) {
  40. outMsg["current_c"] = inMsg.current_c;
  41. }
  42. };
  43. // 4.调用字段筛选规则的函数
  44. outMsg.filterField();
  45. // 输出对象
  46. console.log(outMsg)

水表-遥测数据字段筛选规则

  1. // 1.接收到上传的遥测数据
  2. const msg = {
  3. water_consumption: "water_consumption"
  4. };
  5. // 2.创建遥测数据的模板
  6. const outMsg = {
  7. filterField: function () {
  8. }
  9. };
  10. // 3.编写遥测数据的字段筛选规则
  11. outMsg.filterField = function () {
  12. // 判断某个`key`是否存在。true:则入库;false:则移除
  13. // 水表读数
  14. if (msg.hasOwnProperty("water_consumption")) {
  15. outMsg["water_consumption"] = msg.water_consumption;
  16. }
  17. };
  18. // 4.调用字段筛选规则的函数
  19. outMsg.filterField();
  20. // 输出对象
  21. console.log(outMsg)