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到31yyyy:四位数表示的年份hh:小时数,从 0 到 23 的整数mm:分钟数,从 0 到 59 的整数ss:秒数,从0到59的整数ms:毫秒数,为大于等于0的整数
判断指定字符串是否是一个 URL
判断指定字符串是否是一个 URL。这段代码定义了一个 isUrl() 方法,方法内部使用正则表达式来验证参数字符串是否是符合 URL 规则;同时,还演示了如何测试我们定义的 isUrl() 方法。
// 检测是否是合法的 URLfunction isUrl(url) {const regex = /\b(https?):\/\/[\-A-Za-z0-9+&@#\/%?=~_|!:,.;]*[\-A-Za-z0-9+&@#\/%=~_|]/i;return regex.test(url);}// 测试代码let url = "https://www.dute.org";let r = isUrl(url);// 输出结果console.log(url);console.log(r);
TypeScript中生产uuid的四种方式:
- 方式一:
```typescript
function generateUUID() {
return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(/[xy]/g, function (c) {
}); }const r = Math.random() * 16 | 0,v = c == 'x' ? r : (r & 0x3 | 0x8);return v.toString(16);
// “a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7” const uuid = generateUUID();
- 方式二:```typescriptfunction generateUUID() {const s = [];const hexDigits = "0123456789abcdef";for (let i = 0; i < 36; i++) {s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);}s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01s[8] = s[13] = s[18] = s[23] = "-";return s.join("");}// "a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7"const uuid = generateUUID();
- 方式三:
```typescript
function generateUUID() {
function gen() {
} return (gen() + gen() + “-“ + gen() + “-“ + gen() + “-“ + gen() + “-“ + gen() + gen() + gen()); }return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
// “a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7” const uuid = generateUUID();
- 方式四:```typescript// 指定长度和基数function generateUUID(len, radix) {const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');let uuid = [], i;radix = radix || chars.length;if (len) {// Compact formfor (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];} else {// rfc4122, version 4 formlet r;// rfc4122 requires these charactersuuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';uuid[14] = '4';// Fill in random data. At i==19 set the high bits of clock sequence as// per rfc4122, sec. 4.1.5for (i = 0; i < 36; i++) {if (!uuid[i]) {r = 0 | Math.random() * 16;uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];}}}return uuid.join('');}// "277571702EE33E11"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();
}
- 获取每个月的最后一天```javascript/*由于JavaScript中day的范围为1~31中的值,所以当设为0时,会向前 一天,也即表示上个月的最后一天。所以: new Date(2017,2,0).getDate() 会获取到`2`月份的最后一天*/function mGetDate(year, month){var d = new Date(year, month, 0);return d.getDate();}var totalDay = mGetDate(2004,9); //30console.log(totalDay)
去除字符串两边的空格
var str:string = "\n\n\t\t dfd \n\n\t\t";str = str.replace(/(^\s*)|(\s*$)/g, "")// 输出对象console.log(str);console.log(str.length);
电表-遥测数据字段筛选规则
// 1.接收到上传的遥测数据var inMsg = {import_active_energy: "import_active_energy",voltage_a: "voltage_a",voltage_b: "voltage_b",voltage_c: "voltage_c",current_a: "current_a",current_b: "current_b",current_c: "current_c",cycle_delta_usage: "cycle_delta_usage",day_delta_usage: "day_delta_usage",month_delta_usage: "month_delta_usage",};// 2.创建遥测数据的模板var outMsg = {filterField: function () { }};// 3.编写遥测数据的字段筛选规则outMsg.filterField = function () {// 判断某个`key`是否存在。true:则入库;false:则移除if (inMsg.hasOwnProperty("import_active_energy")) {outMsg["import_active_energy"] = inMsg.import_active_energy;}if (inMsg.hasOwnProperty("voltage_a")) {outMsg["voltage_a"] = inMsg.voltage_a;}if (inMsg.hasOwnProperty("voltage_b")) {outMsg["voltage_b"] = inMsg.voltage_b;}if (inMsg.hasOwnProperty("voltage_c")) {outMsg["voltage_c"] = inMsg.voltage_c;}if (inMsg.hasOwnProperty("current_a")) {outMsg["current_a"] = inMsg.current_a;}if (inMsg.hasOwnProperty("current_b")) {outMsg["current_b"] = inMsg.current_b;}if (inMsg.hasOwnProperty("current_c")) {outMsg["current_c"] = inMsg.current_c;}};// 4.调用字段筛选规则的函数outMsg.filterField();// 输出对象console.log(outMsg)
水表-遥测数据字段筛选规则
// 1.接收到上传的遥测数据const msg = {water_consumption: "water_consumption"};// 2.创建遥测数据的模板const outMsg = {filterField: function () {}};// 3.编写遥测数据的字段筛选规则outMsg.filterField = function () {// 判断某个`key`是否存在。true:则入库;false:则移除// 水表读数if (msg.hasOwnProperty("water_consumption")) {outMsg["water_consumption"] = msg.water_consumption;}};// 4.调用字段筛选规则的函数outMsg.filterField();// 输出对象console.log(outMsg)
