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 ...
随机推荐
- PGSQL 获取数据库大小以及表达小等的SQL
SELECT d.datname AS Name, pg_catalog.pg_get_userbyid(d.datdba) AS Owner, CASE WHEN pg_catalog.has_da ...
- 【转帖】Systemd 入门教程:命令篇
Systemd 入门教程:命令篇 Copy From http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html 感觉 ...
- Fantacy团队周二站立会议
词频分析模型 1.这次站会是周二开的,但是由于我个人的疏忽,哎,不说了. 2.会议时间:2016年3月29日12:03~12:30. 持续时长:27分钟 会议参加成员:组长:杨若鹏 http://ww ...
- 软件工程_2nd weeks
本周上课没有板书,都由老师口头叙述 因此有的笔记记得不是很全,幸好有郑蕊师姐发布的课堂笔记,很好的梳理上课的内容~ 1.根据老师上课给的建议,进行了深刻的思考和反思 1.1 作为一个学硕研究生,这门课 ...
- js regex variable & Set, Map
js regex variable & Set, Map regex, variable, Set, Map, 交集, 差集, 并集, https://stackoverflow.com/qu ...
- delphi try except语句 和 try finally语句用法以及区别
try//尝试执行{SomeCode} except//出错的时候执行, Except有特定的错误类型 {SomeCode} end; try//尝试执行{SomeCode} finally//无论如 ...
- python之文件读写和异常处理
# 文件读取.写入和异常处理操作举例 # date:2017-07-17 file_name = "D:/file_demo.txt" with open(file_name, ' ...
- ansible系列6-用户管理
第一种:新增用户 ansible host -m user -a "name=zhang shell=/bin/bash groups=admin,root append=yes home= ...
- BZOJ2144跳跳棋——LCA+二分
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的 游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...
- lambda 分类抽取N条
从每个分类中,按照比例抽取 lista中percent是比例,按照这个比例抽取. static List<A> lista = new List<A>() { new A(){ ...