Skip to content

Instantly share code, notes, and snippets.

@vitalibondar
Created May 17, 2020 09:54
Show Gist options
  • Save vitalibondar/b40679c8ef7afcac7cb1a90715f9cf72 to your computer and use it in GitHub Desktop.
Save vitalibondar/b40679c8ef7afcac7cb1a90715f9cf72 to your computer and use it in GitHub Desktop.
var diffDate = function (date, diff) {
// date: '1993-09-11'
// diff: +1, -1
const dateHandling = () => {
const askDate = prompt('Enter date: ', 'YYYYMMDD or YYYY MM DD');
if (askDate === null) return new Date();
const dates = (str) => {
const year = str.slice(0, 4);
const month = str.slice(4, 6) - 1;
const day = str.slice(6, 8);
return [year, month, day];
};
if (askDate.match(/\d{8}/)) {
return new Date(...dates(askDate));
}
if (askDate.match(/[0-9]{4}(([-:\s])[0-9]{2}){2}/)) {
const match = /[0-9]{4}(([-:\s])[0-9]{2}){2}/.exec(askDate);
const dateString = askDate.split(match[2]).join('');
return new Date(...dates(dateString));
}
return new Date();
};
const d = dateHandling();
d.setDate(d.getDate() + diff);
var a = [
d.getFullYear(),
('00' + (1 + d.getMonth())).slice(-2),
('00' + d.getDate()).slice(-2)
];
return a.join('-');
};
const main = function () {
var days = ["неділя", "понеділок", "вівторок", "середа", "четвер", "пʼятниця", "субота"];
var monthNames = ["січень", "лютий", "березень", "квітень", "травень", "червень",
"липень", "серпень", "вересень", "жовтень", "листопад", "грудень"]; //масив з локалізованими місяцями
var d = new Date();
var year = d.getFullYear();
var dt = d.getDate();
var month = d.getMonth() + 1;
var date = [
year,
('00' + month).slice(-2),
('00' + dt).slice(-2)
];
var day = days[d.getDay()];
/****** 編集エリア ******/
// お好きなタイトル
var title = date.join('-');
// var monthLocal = date.toLocaleString('uk-UA', { month: 'long' }); //не працює
/************************/
var scrapboxProject = location.href.match(/scrapbox.io\/([^\/.]*)/)[1];
var projectUrl = 'https://scrapbox.io/' + scrapboxProject + '/';
var tags = [
'[← ' + projectUrl + encodeURIComponent(diffDate(title, -1)) +']',
'[' + year + ' рік' + ']' + ',',
// '[' + month + '-й місяць' + ']' + ',',
'[' + monthNames[d.getMonth()] + ']' + ',',
// '[' + monthLocal + ']' + ',', //не працює
'[' + day + '' + ']',
'[→ ' + projectUrl + encodeURIComponent(diffDate(title, +1)) +']'
];
/****** 編集エリア ******/
// お好きなハッシュタグまたは本文
var body = encodeURIComponent('[* Справи:]' + '\n' + '[* Що сталося за день:]' + '\n' + '[* Цікаві думки та ідеї:]' + '\n' + tags.join(' ') + '\n' + '#Щоденник');
/************************/
var scrapboxUrl = 'https://scrapbox.io/' + scrapboxProject + '/' + encodeURIComponent(title);
window.open(scrapboxUrl + '?body=' + body);
}
main()
@i386net
Copy link

i386net commented May 17, 2020

Попробуй вот так:

var diffDate = function(date, diff) {
  // date: '1993-09-11'
  // diff: +1, -1
  date.setDate(date.getDate() + diff);
  var a = [
    date.getFullYear(),
    ('00' + (1 + date.getMonth())).slice(-2),
    ('00' + date.getDate()).slice(-2),
  ];
  return a.join('-');
};

const main = function() {
  var days = [
    'неділя',
    'понеділок',
    'вівторок',
    'середа',
    'четвер',
    'пʼятниця',
    'субота',
  ];
  var monthNames = [
    'січень',
    'лютий',
    'березень',
    'квітень',
    'травень',
    'червень',
    'липень',
    'серпень',
    'вересень',
    'жовтень',
    'листопад',
    'грудень',
  ]; //масив з локалізованими місяцями
  const dateHandling = () => {
    const askDate = prompt('Enter date: ', 'YYYYMMDD or YYYY MM DD');
    if (askDate === null) return new Date();
    const dates = (str) => {
      const year = str.slice(0, 4);
      const month = str.slice(4, 6) - 1;
      const day = str.slice(6, 8);
      return [year, month, day];
    };
    if (askDate.match(/\d{8}/)) {
      return new Date(...dates(askDate));
    }
    if (askDate.match(/[0-9]{4}(([-:\s])[0-9]{2}){2}/)) {
      const match = /[0-9]{4}(([-:\s])[0-9]{2}){2}/.exec(askDate);
      const dateString = askDate.split(match[2]).join('');
      return new Date(...dates(dateString));
    }

    return new Date();
  };
  var d = dateHandling();
  var year = d.getFullYear();
  var dt = d.getDate();
  var month = d.getMonth() + 1;
  var date = [year, ('00' + month).slice(-2), ('00' + dt).slice(-2)];
  var day = days[d.getDay()];
  /****** 編集エリア ******/
  // お好きなタイトル
  var title = date.join('-');
  // var monthLocal = date.toLocaleString('uk-UA', { month: 'long' }); //не працює
  /************************/
  var scrapboxProject = location.href.match(/scrapbox.io\/([^\/.]*)/)[1];
  var projectUrl = 'https://scrapbox.io/' + scrapboxProject + '/';
  var tags = [
    '[← ' + projectUrl + encodeURIComponent(diffDate(title, -1)) + ']',
    '[' + year + ' рік' + ']' + ',',
    // '[' + month + '-й місяць' + ']' + ',',
    '[' + monthNames[d.getMonth()] + ']' + ',',
    // '[' + monthLocal + ']' + ',', //не працює
    '[' + day + '' + ']',
    '[→ ' + projectUrl + encodeURIComponent(diffDate(title, +1)) + ']',
  ];

  /****** 編集エリア ******/
  // お好きなハッシュタグまたは本文
  var body = encodeURIComponent(
    '[* Справи:]' +
      '\n' +
      '[* Що сталося за день:]' +
      '\n' +
      '[* Цікаві думки та ідеї:]' +
      '\n' +
      tags.join(' ') +
      '\n' +
      '#Щоденник'
  );
  /************************/

  var scrapboxUrl =
    'https://scrapbox.io/' + scrapboxProject + '/' + encodeURIComponent(title);
  window.open(scrapboxUrl + '?body=' + body);
};

@i386net
Copy link

i386net commented May 17, 2020

А так?

const  diffDate = (date, diff) =>  {
   // date: '1993-09-11'
   // diff: +1, -1
   date.setDate(date.getDate() + diff);
   const a = [
     date.getFullYear(),
     ('00' + (1 + date.getMonth())).slice(-2),
     ('00' + date.getDate()).slice(-2),
   ];
   console.log(a);
   return a.join('-');
 };
 
 const main = function() {
   const days = [
     'неділя',
     'понеділок',
     'вівторок',
     'середа',
     'четвер',
     'пʼятниця',
     'субота',
   ];
   const monthNames = [
     'січень',
     'лютий',
     'березень',
     'квітень',
     'травень',
     'червень',
     'липень',
     'серпень',
     'вересень',
     'жовтень',
     'листопад',
     'грудень',
   ]; //масив з локалізованими місяцями
   const dateHandling = () => {
     const askDate = prompt('Enter date: ', 'YYYYMMDD or YYYY MM DD');
     if (askDate === null) return new Date();
     const dates = (str) => {
       const year = str.slice(0, 4);
       const month = str.slice(4, 6) - 1;
       const day = str.slice(6, 8);
       return [year, month, day];
     };
     if (askDate.match(/\d{8}/)) {
       return new Date(...dates(askDate));
     }
     if (askDate.match(/[0-9]{4}(([-:\s])[0-9]{2}){2}/)) {
       const match = /[0-9]{4}(([-:\s])[0-9]{2}){2}/.exec(askDate);
       const dateString = askDate.split(match[2]).join('');
       return new Date(...dates(dateString));
     }
 
     return new Date();
   };
   const d = dateHandling(); 
   const year = d.getFullYear();
   const dt = d.getDate();
   const month = d.getMonth() + 1;
   const date = [year, ('00' + month).slice(-2), ('00' + dt).slice(-2)];
   const day = days[d.getDay()];
   /****** 編集エリア ******/
   // お好きなタイトル
   const title = date.join('-');
   console.log(title)
   // var monthLocal = date.toLocaleString('uk-UA', { month: 'long' }); //не працює
   /************************/
   const scrapboxProject = location.href.match(/scrapbox.io\/([^\/.]*)/)[1];
   const projectUrl = 'https://scrapbox.io/' + scrapboxProject + '/';
   const tags = [
     '[← ' + projectUrl + encodeURIComponent(diffDate(d, -1)) + ']',
     '[' + year + ' рік' + ']' + ',',
     // '[' + month + '-й місяць' + ']' + ',',
     '[' + monthNames[d.getMonth()] + ']' + ',',
     // '[' + monthLocal + ']' + ',', //не працює
     '[' + day + '' + ']',
     '[→ ' + projectUrl + encodeURIComponent(diffDate(d, +1)) + ']',
   ];
 
   /****** 編集エリア ******/
   // お好きなハッシュタグまたは本文
   const body = encodeURIComponent(
     '[* Справи:]' +
       '\n' +
       '[* Що сталося за день:]' +
       '\n' +
       '[* Цікаві думки та ідеї:]' +
       '\n' +
       tags.join(' ') +
       '\n' +
       '#Щоденник'
   );
   /************************/
 
   const scrapboxUrl =
     'https://scrapbox.io/' + scrapboxProject + '/' + encodeURIComponent(title);
   window.open(scrapboxUrl + '?body=' + body);
 };
 main();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment