add parseDate to get Date object from any date or time value

This commit is contained in:
Star 2025-08-05 16:39:31 +08:00
parent b184e96f64
commit f1528bb80c
4 changed files with 23 additions and 14 deletions

4
go.mod
View File

@ -1,9 +1,9 @@
module apigo.cc/gojs/util
go 1.23.0
go 1.24
require (
apigo.cc/gojs v0.0.23
apigo.cc/gojs v0.0.25
apigo.cc/gojs/console v0.0.2
github.com/ZZMarquis/gm v1.3.2
github.com/emmansun/gmsm v0.30.1

View File

@ -838,6 +838,10 @@ func init() {
TsCode: utilTS,
Example: utilMD,
JsCode: `
$MOD$.parseDate = function (v) {
return new Date($MOD$.timestampMS(v))
}
$MOD$.keysBy = function (obj, ...fieldAndValues) {
let keys = []
for (let k in obj) {

15
util.ts
View File

@ -59,12 +59,12 @@ export default {
hmacSM3,
tpl,
formatDate,
addDate,
timestampMS,
timestamp,
toDatetime,
fromDatetime,
toDate,
fromDate,
parseDate,
joinPath,
getPathDir,
getPathBase,
@ -165,12 +165,11 @@ function hmacSM3(key: any, ...data: any[]): any { return '' }
function tpl(text: string, data: any, functions?: Object): string { return '' }
function formatDate(format: string, value?: any): string { return '' }
function addDate(add: string, value?: any): string { return '' }
function timestampMS(): number { return 0 }
function timestamp(): number { return 0 }
function toDatetime(timestamp?: number): string { return '' }
function fromDatetime(datetimeStr: string): number { return 0 }
function toDate(timestamp?: number): string { return '' }
function fromDate(dateStr: string): number { return 0 }
function timestampMS(value?: any): number { return 0 }
function timestamp(value?: any): number { return 0 }
function toDatetime(value?: any): string { return '' }
function toDate(value?: any): string { return '' }
function parseDate(value: any): Date { return new Date() }
function joinPath(...paths: string[]): string { return '' }
function getPathDir(path: string): string { return '' }
function getPathBase(path: string): string { return '' }

View File

@ -37,10 +37,6 @@ func TestHash(t *testing.T) {
sm4R, _ := vm.RunCode("util.hex(util.sm4('hello 123','12345678901234567','12345678901234567'))")
testIsSame(vm, t, "util.unSM4('"+u.String(sm4R)+"','12345678901234567','12345678901234567')", "hello 123")
testIsSame(vm, t, "util.gzip('hello 123')", u.GzipN([]byte("hello 123")))
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", "2024-01-01 00:00:00", time.Local)
testIsSame(vm, t, "util.timestampMS('2024-01-01 00:00:00')", tm.UnixMilli())
testIsSame(vm, t, "util.timestampMS('Mon Jan 01 2024 00:00:00 GMT+0800 (中国标准时间)')", tm.UnixMilli())
testIsSame(vm, t, "util.timestampMS(new Date("+u.String(tm.UnixMilli())+"))", tm.UnixMilli())
}
func TestECDSA(t *testing.T) {
@ -127,6 +123,16 @@ func TestRSA(t *testing.T) {
fmt.Println(u.Green("ecdsa test passed"))
}
func TestDate(t *testing.T) {
vm := gojs.New()
vm.RunCode("import util from 'apigo.cc/gojs/util'\nimport co from 'apigo.cc/gojs/console'")
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", "2024-01-01 00:00:00", time.Local)
testIsSame(vm, t, "util.timestampMS('2024-01-01 00:00:00')", tm.UnixMilli())
testIsSame(vm, t, "util.timestampMS('Mon Jan 01 2024 00:00:00 GMT+0800 (中国标准时间)')", tm.UnixMilli())
testIsSame(vm, t, "util.timestampMS(new Date("+u.String(tm.UnixMilli())+"))", tm.UnixMilli())
testIsSame(vm, t, "util.parseDate(new Date('Mon Jan 01 2024 00:00:00 GMT+0800 (中国标准时间)')).getTime()", tm.UnixMilli())
}
func testIsSame(vm *gojs.Runtime, t *testing.T, code string, checkValue any) {
r, err := vm.RunCode(code)
if err != nil {