简体   繁体   中英

Convert Filename to Timestamp using Node.js

I have to convert a filename to a timestamp for my laravel application.

let test = "/2020-05-16_11-17-47_UTC_1.jpg"

i tried to replace the - with split and join but i dont think its the best option.. It has to look like this: 2020-05-16 11:17:47 .

I tried it like this:

  let regex = test.toString().match('/[^/]*$')[0]
  let upload_date = regex.split("/").join("").split(".jpg").join("").split(".mp4").join("").split("_").join(" ")

Im struggling with replacing the : ...

You don't need to over kill split() method in order to get the expected result.

let str = '/home/insta/data/name/2020-05-16_11-17-47_UTC_1.jpg';
let chunks = str.split('/');    
let formatted = chunks[chunks.length-1].replace(/[^0-9_-]/g, '').replace(/_/g,' ').slice(0,19);
console.log(formatted);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM