Home > front end >  How can set the filename to today's date? window.saveAs(blob, 'Time');
How can set the filename to today's date? window.saveAs(blob, 'Time');

Time:11-30

How can set the filename to today's date?

$("#download").on("click", function() {
  html2canvas(document.querySelector("#to_save")).then(canvas => {
    canvas.toBlob(function(blob) {
      window.saveAs(blob, #);
    });
  });
});

I couldn't find a solution.

CodePudding user response:

I found the solution

$( "#download" ).on( "click", function() {
          html2canvas(document.querySelector("#to_save")).then(canvas => {
            canvas.toBlob(function(blob) {
              window.saveAs(blob, getFormattedTime());
              function getFormattedTime() {
                    var today = new Date();
                    var y = today.getFullYear();
                    // JavaScript months are 0-based.
                    var m = today.getMonth()   1;
                    var d = today.getDate();
                    return y   "-"   m   "-"   d;
              }
            });
            });
        });

CodePudding user response:

What format did you want the time in?

If you want YYYY-MM-DD, you can use toLocaleDateString set the locale to Swedish (Sweden) i.e. sv-SE. For more information on date formatting, see Intl.DateTimeFormat.

(function($) {
  $.todaysDate = function() {
    return new Date().toLocaleDateString('sv-SE');
  };
})(jQuery);

$("#download").on("click", function() {
  html2canvas(document.querySelector("#to_save")).then(canvas => {
    canvas.toBlob(function(blob) {
      window.saveAs(blob, $.todaysDate());
    });
  });
});

console.log($.todaysDate()); // Today's date in YYYY-MM-DD
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Here are all the locales for comparison:

const now = new Date();

const datesByLocale = {
  'ar-SA': now.toLocaleDateString('ar-SA'),
  'bn-BD': now.toLocaleDateString('bn-BD'),
  'bn-IN': now.toLocaleDateString('bn-IN'),
  'cs-CZ': now.toLocaleDateString('cs-CZ'),
  'da-DK': now.toLocaleDateString('da-DK'),
  'de-AT': now.toLocaleDateString('de-AT'),
  'de-CH': now.toLocaleDateString('de-CH'),
  'de-DE': now.toLocaleDateString('de-DE'),
  'el-GR': now.toLocaleDateString('el-GR'),
  'en-AU': now.toLocaleDateString('en-AU'),
  'en-CA': now.toLocaleDateString('en-CA'),
  'en-GB': now.toLocaleDateString('en-GB'),
  'en-IE': now.toLocaleDateString('en-IE'),
  'en-IN': now.toLocaleDateString('en-IN'),
  'en-NZ': now.toLocaleDateString('en-NZ'),
  'en-US': now.toLocaleDateString('en-US'),
  'en-ZA': now.toLocaleDateString('en-ZA'),
  'es-AR': now.toLocaleDateString('es-AR'),
  'es-CL': now.toLocaleDateString('es-CL'),
  'es-CO': now.toLocaleDateString('es-CO'),
  'es-ES': now.toLocaleDateString('es-ES'),
  'es-MX': now.toLocaleDateString('es-MX'),
  'es-US': now.toLocaleDateString('es-US'),
  'fi-FI': now.toLocaleDateString('fi-FI'),
  'fr-BE': now.toLocaleDateString('fr-BE'),
  'fr-CA': now.toLocaleDateString('fr-CA'),
  'fr-CH': now.toLocaleDateString('fr-CH'),
  'fr-FR': now.toLocaleDateString('fr-FR'),
  'he-IL': now.toLocaleDateString('he-IL'),
  'hi-IN': now.toLocaleDateString('hi-IN'),
  'hu-HU': now.toLocaleDateString('hu-HU'),
  'id-ID': now.toLocaleDateString('id-ID'),
  'it-CH': now.toLocaleDateString('it-CH'),
  'it-IT': now.toLocaleDateString('it-IT'),
  'ja-JP': now.toLocaleDateString('ja-JP'),
  'ko-KR': now.toLocaleDateString('ko-KR'),
  'nl-BE': now.toLocaleDateString('nl-BE'),
  'nl-NL': now.toLocaleDateString('nl-NL'),
  'no-NO': now.toLocaleDateString('no-NO'),
  'pl-PL': now.toLocaleDateString('pl-PL'),
  'pt-BR': now.toLocaleDateString('pt-BR'),
  'pt-PT': now.toLocaleDateString('pt-PT'),
  'ro-RO': now.toLocaleDateString('ro-RO'),
  'ru-RU': now.toLocaleDateString('ru-RU'),
  'sk-SK': now.toLocaleDateString('sk-SK'),
  'sv-SE': now.toLocaleDateString('sv-SE'),
  'ta-IN': now.toLocaleDateString('ta-IN'),
  'ta-LK': now.toLocaleDateString('ta-LK'),
  'th-TH': now.toLocaleDateString('th-TH'),
  'tr-TR': now.toLocaleDateString('tr-TR'),
  'zh-CN': now.toLocaleDateString('zh-CN'),
  'zh-HK': now.toLocaleDateString('zh-HK'),
  'zh-TW': now.toLocaleDateString('zh-TW'),
};

const formatToLocales = _.invertBy(datesByLocale);

console.log(formatToLocales);
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

From the example above, we can see that the following locales provide the YYYY-MM-DD date format:

  • en-CA ~ English (Canada)
  • fr-CA ~ French (Canada)
  • sv-SE ~ Swedish (Sweden)

See also: http://www.lingoes.net/en/translator/langcode.htm

  • Related