js 格式化时间、字符串指定长度、随机字符串
格式化字符串长度
方法
function formatWidth(str, width){
str += ''
if(str.length<width)
return formatWidth(''+str, width)
else
return str
}
测试代码
a =
console.log(a,formatWidth(a,))
a =
console.log(a,formatWidth(a,))
a =
console.log(a,formatWidth(a,))
运行结果

获取格式化时间字符串
方法
function timeFormat(inTime, formatStr, getDate){
formatStr = formatStr || 'Y-M-D H:m:S'
let weeks = ['日','一','二','三','四','五','六']
let formater = {
Y: inTime.getFullYear(),
M: formatWidth(inTime.getMonth()+,),
D: formatWidth(inTime.getDate(),),
H: formatWidth(inTime.getHours(),),
m: formatWidth(inTime.getMinutes(),),
S: formatWidth(inTime.getSeconds(),),
W: '星期'+weeks[inTime.getDay()]
}
for(let i in formater)
formatStr = formatStr.replace(i, formater[i])
return getDate ? new Date(formatStr) : formatStr
}
测试代码
console.dir(timeFormat(new Date(), 'Y-M-D H:m:S'))
console.dir(timeFormat(new Date(), 'YMDHmS'))
console.dir(timeFormat(new Date(), 'Y H:m:S'))
console.dir(timeFormat(new Date(), 'Y-M-D'))
测试结果

随机字符串
方法
function randomString(len) {
len = len || ;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
var maxPos = $chars.length;
var pwd = '';
for (i = ; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
测试代码
console.log(randomString())
console.log(randomString())
console.log(randomString())
console.log(randomString())
测试结果

js 格式化时间、字符串指定长度、随机字符串的更多相关文章
- js格式化时间(指定模板格式)
1 /** 格式化时间 2 * @param {string} date 需要格式化的时间 3 * @param {string} fmt 想要格式化的格式 4 */ 5 function forma ...
- 随机生成指定长度字符字符串(C语言实现)
相关函数 srand(), rand()头文件#include<stdlib.h> 定义函数 int rand(void) 函数说明 rand()会返回一随机数值,范围在0至RAND_MA ...
- asp.net截取指定长度的字符串内容
/// <summary> /// 用于截取指定长度的字符串内容 /// </summary> /// <param name="sString"&g ...
- SQLServer中查询的数字列前面补0返回指定长度的字符串
SQLServer中查询的数字列前面补0返回指定长度的字符串: 如: 角本如下: /****** Script for SelectTopNRows command from SSMS ******/ ...
- C#按指定长度分割字符串
C#按指定长度分割字符串 这几天学习分析声音的波形数据,接收到的是十六进制的数据,需要将数据转换成十进制再绘图,这个过程涉及到字符串的分割,正好可以促进自己对C#相关知识的学习.说到分割字符串,我 ...
- 在smarty模板中截取指定长度的字符串
在smarty模板中截取指定长度的字符串,可使用truncate这个插件. 用法: {{$data.value|truncate:28:'...'}} 28个字节14个字数输出,多余部分输出...,一 ...
- js格式化时间的方法
方法一:用js格式化时间的方法. Date.prototype.format =function(format) { var o = { "M+" : this.getMonth( ...
- PHP字符串指定位置插入字符串
1.substr_replace(string,replacement,start,length);需插入时设置length为0即可 string 必需.规定要检查的字符串. replacement ...
- java随机生成简体中文取指定长度随机简体中文实用方法
/** * 获取指定长度随机简体中文 * @param len int * @return String */ public static String getR ...
- JS格式化时间并比较
JS格式化时间,然后进行比较.工作遇到的情况,然后网上找到的,记下来,下次用! </head> <body> <button onclick="myFuncti ...
随机推荐
- React 多组件传值props和this
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 电梯间的谈话:3分钟快速回答CEO的问题
想象一下,你在电梯里遇见了公司的CEO,他让你用3分钟来解释聚焦答案模式这个深奥的术语到底是什么意思.你可以这么说—— 为奠定一个正确的基调,让他酝酿好情绪听你说接下来的话,你可以这样开头: “总裁, ...
- 软件工程_6th weeks
一.上次博客时说的UI,拖拉到现在才展示,完成了“登录,普通匹配,做题界面,做题结果”四项 功能: 二.单元测试工具 1.python单元测试工具 最近因为论文原因一直在用Python,Pytho ...
- delphi try except语句 和 try finally语句用法以及区别
try//尝试执行{SomeCode} except//出错的时候执行, Except有特定的错误类型 {SomeCode} end; try//尝试执行{SomeCode} finally//无论如 ...
- LoadRunner12 Java Vuser API语法举例
// 检查点 web.reg_find("Text=\"retCode\":\"0000\"",new String[]{"FAI ...
- linux shell脚本报错总结
1 rizhi.sh: line 28: warning: here-document at line 9 delimited by end-of-file (wanted `EOF') 原因是末尾 ...
- BZOJ1468Tree——点分治
题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入 N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k 输出 一行,有多 ...
- c# Point不能输入小数
换成用 PointF PointF p = new PointF(116.305671f, 39.966051f); //6位小数后面要加f 表示转float,否则报错
- 洛谷 P4148 简单题 解题报告
P4148 简单题 题意 维护单点加与矩形求和,强制在线 说明 \(n\le 500000,m\le 200000\),\(4000ms / 20MB\) kd-tree 复杂度我不懂 是一颗平衡树, ...
- Android中用文件初始化sqlite 数据库(二)
博 androidsqlite启动时数据库初始化 方法1已经讲述了一种初始化数据库的方法 它的数据库初始化不是用sql语句,而是用一个现成的sqlite的二进制文件进行直接copy到Android系 ...