Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to get file's last modified date with script?

New Here ,
Jan 06, 2019 Jan 06, 2019

How to get file's last modified date with script?

TOPICS
Scripting
5.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jan 06, 2019 Jan 06, 2019

Sure

alert(myFile.modified.getTime());

Translate
Advocate ,
Jan 06, 2019 Jan 06, 2019

File object has a specific property modified, that you want to look at.

var myFile = File('pathToFile.js');

alert(myFile.modified);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 06, 2019 Jan 06, 2019

great! it works!   but can it transform to other format, like 201901160102  ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 06, 2019 Jan 06, 2019

Sure

alert(myFile.modified.getTime());

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 06, 2019 Jan 06, 2019

THANKS!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 06, 2019 Jan 06, 2019
LATEST

Actually, I just re-read your second reply, where you want to format time. Here's one approach to that. Feel free to juggle around to format it your way. Also read more about Date() format here Date | MDN

var myFile = File('pathToFile.js');

var modified = myFile.modified;

var formatedTime = formatDate(modified);

alert(formatedTime);

function formatDate(date) {

    var year = date.getFullYear();

    var month = date.getMonth() + 1; // In JS world month starts at 0, not 1;

    var day = date.getDate();

    var hours = date.getHours();

    var minutes = date.getMinutes();

    var seconds = date.getSeconds();

    var result = year + pad(month) + pad(day) + pad(hours) + pad(minutes) + pad(seconds);

    return result;

}

function pad(value, padding) {

    padding = padding || 2;

    var string = value.toString();

    while (string.length < 2) {

        string = '0' + string;

    }

    return string;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines