Snippets Collections
#include<iostream>
using namespace std;
class DATE{
    int day;
    int month;
    int year;
    public:
    DATE(){}
    DATE(char*);
    int operator-(DATE);
    DATE operator+(int);
    bool operator<(DATE);
    friend ostream& operator<<(ostream &,DATE&);
    ~DATE();
};
int main(){

    cout<<"Enter the first date in dd/mm/yyyy format: ";
    char input[20];
    cin>>input;
    DATE d1(input);
    cout<<d1<<endl;
    cout<<"Enter the second date in dd/mm/yyyy format: ";
    cin>>input;
    DATE d2(input);
    cout<<d2<<endl;
    int num_days = d2-d1;
    
    cout<<"Number of days in between is: "<<num_days<<endl;
    cout<<"Enter the number of days\n";
    int num;
    cin>>num;
    DATE d3 = d1 + num;
    cout<<d1<<" + "<<num<<" = "<<d3;
    return 0;
}
int my_stoi(char *input,int start,int len){
    int output =0;
    for(int i=start;i<start+len;i++){
        output = output*10 + input[i]-'0';
    }
    return output;
}
DATE::DATE(char *input){
    day = my_stoi(input,0,2);
    month = my_stoi(input,3,2);
    year = my_stoi(input,6,4);
}
inline bool is_leap_year(int yy){
    if(yy%400==0) return 0;
    return yy%4 ? 0: 1;
}
inline int days_in_year(int yy){
    return is_leap_year(yy) ? 366 : 365;
}
int days_in_month(int mo,int yy){
    switch(mo){
        case 1:case 3:case 5: case 7: case 8: case 10: case 12:
            return 31;
        case 4: case 6: case 9: case 11:
            return 30;
        case 2: 
            if(is_leap_year(yy)) return 29;
            else return 28;
    }
    return -1;
}
bool DATE::operator<(DATE d2){
    if(year<d2.year) return true;
    else if(year>d2.year) return false;
    if(month<d2.month) return true;
    else if(month>d2.month) return false;
    return day<d2.day;
}
int DATE::operator-(DATE d2){
    if(*this<d2) return d2-*this;

    int prefix_days_d1 =0;
    for(int i=1;i<month;i++) prefix_days_d1 += days_in_month(i,year);
    prefix_days_d1 += day;

    int suffix_days_d2=0;
    suffix_days_d2 += days_in_month(d2.month,d2.year)-d2.day;
    for(int i = d2.month+1;i<=12;i++) suffix_days_d2 += days_in_month(i,d2.year);

    int difference = suffix_days_d2 + prefix_days_d1;

    for(int i = d2.year+1 ; i<year ; i++){
        difference += days_in_year(i);
    }
    if(year==d2.year) difference -= days_in_year(year);   // overlap
    return difference;
}
ostream& operator<<(ostream &print,DATE &obj){
    if(obj.day/10 ==0) print<<"0";
    print<<obj.day;
    print<<"/";
    if(obj.month/10 ==0) print<<"0";
    print<<obj.month<<"/";
    print<<obj.year;
    return print;
}
DATE DATE::operator+(int add_days){
    DATE output;
    int dd = day;
    int mm = month;
    int yy = year;
    int current_days = days_in_month(mm,yy) - dd;
    if(add_days > current_days){  
                                    // rounding up the month
        add_days-=current_days;
        mm++;
        if(mm==13) {
            mm=1;
            yy++;
        }
    }
    else{
        dd +=add_days;
        add_days=0;
    }
    while(days_in_month(mm,yy)<add_days){
                                        // rounding up the year
        add_days-=days_in_month(mm,yy);
        mm++;
        if(mm==13) {
            mm=1;
            yy++;
            break;
        }
    }
    while(days_in_year(yy)<add_days){   //locating to the year
        add_days-=days_in_year(yy);
        yy++;
    }
    while(days_in_month(mm,yy)<add_days){ // locating to the month
        add_days-=days_in_month(mm,yy);
        mm++;
    }
    if(add_days!=0) dd = add_days;  // locating to the date
    output.day = dd;
    output.month = mm;
    output.year = yy;

    return output;
}
DATE::~DATE(){
    
}
  private String getFormatedDate(long time){
        simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String formatedDate = simpleDateFormat.format(time);
        return formatedDate;
    }
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let nameOfMonth = dateFormatter.string(from: now)
const today = new Date()
const oneMonthAgo = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate())
func isDayEnded(recent: Date, previous: Date) -> Bool {
    let day:Int = Calendar.current.dateComponents([.day], from: previous, to: recent).day ?? 0
    if day >= 1 {
        return true
    } else {
        return false
    }
}
let previous = NSDate(timeIntervalSince1970: 1645601459)
let recent = NSDate(timeIntervalSince1970:   1648020659)
let dayEnded = isDayEnded(recent: recent as Date, previous: previous as Date)
print("Day = \(dayEnded)")
    func getDateTime(_ timeStamp : String,_ format: String) -> String {
        var t1 = Int(timeStamp) ?? 1
         t1 = t1/1000
        let date = NSDate(timeIntervalSince1970: TimeInterval(t1))
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        let dateString = dateFormatter.string(from: date as Date)
        print("Date = \(dateString)")
        return dateString
    }
//Convert to UTC date only
{{new Date("timestamp from cdr here"*1000).toISOString()}}
//Convert to UTC time and date
{{new Date("timestamp from cdr here"*1000).toISOString().split('T')[0]}}
//Convert to UTC time and date without milliseconds
{{new Date("timestamp from cdr here"*1000).toISOString().slice(0,-8)}}
//Convert to Local time
{{new Date("timestamp from cdr here"*1000).toLocaleString('en-US',{ timeZone: 'America/Chicago' })}}

//Convert YYYY-MM-DD to YYYY-Month Name-DD
{{$node["SplitInBatches"].json["cdr_date"].split('-')[0]}}/{{new Date($node["SplitInBatches"].json["cdr_date"].split('-')[1]).toLocaleString('default', { month: 'long' })}}/{{$node["SplitInBatches"].json["cdr_date"].split('-')[2]}}
[[!SetLocalDate? &date=`[[+publishedon:date=`%d %B %Y`]]`]]
 
 
 <?php
//SetLocalDate
//Converts date format
//Use: [[SetLocalDate? &date=`[[*publishedon]]`]]
  
    $months = array(1 => "Ιανουαρίου",    //January
                     2 => "Φεβρουαρίου", //February
                     3 => "Μαρτίου",      //March
                     4 => "Απριλίου", //April
                     5 => "Μαΐου", //May
                     6 => "Ιουνίου",         //Jun
                     7 => "Ιουλίου",      //July
                     8 => "Αυγούστου", //August
                     9 => "Σεπτεμβρίου",       //September
                     10=> "Οκτωβρίου",    //October
                     11=> "Νοεμβρίου", //November
                     12=> "Δεκεμβρίου");      //December
  
$mj = date("n", strtotime($date));
  
  
// days on Croatian language
       $days = array(1 => "Δευτέρα", // Monday
             2 => "Τρίτη",      //Thuesday
             3 => "Τετάρτη",     //Wensday
             4 => "Πέμπτη",    //Thursday
             5 => "Παρασκευή",        // Friday
             6 => "Σάββατο",       //Saturday
             7 => "Κυριακή");   //Sunday
  
$dy = date("N", strtotime($date));
  
//Get vars
  
$fullday = $days[$dy]; // For example Monday
  
$month = $months[$mj]; // For example January
  
$day = date("d", strtotime($date)); //For example 28
  
$year = date("Y", strtotime($date));// For example 2012
  
  
//display date format for example:
//Ponedjeljak, 24. Prosinac 2012.
  
//return $fullday.', '.$day.'. '.$month.' '.$year.'.';
return $day.' '.$month.' '.$year.'';
const formatDuration = ms => {
  if (ms < 0) ms = -ms;
  const time = {
    day: Math.floor(ms / 86400000),
    hour: Math.floor(ms / 3600000) % 24,
    minute: Math.floor(ms / 60000) % 60,
    second: Math.floor(ms / 1000) % 60,
    millisecond: Math.floor(ms) % 1000
  };
  return Object.entries(time)
    .filter(val => val[1] !== 0)
    .map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
    .join(', ');
};

// Examples
formatDuration(1001); // '1 second, 1 millisecond'
formatDuration(34325055574); // '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'
star

Sun May 08 2022 04:44:34 GMT+0000 (UTC)

#c++ #oop #date
star

Tue Apr 19 2022 08:05:37 GMT+0000 (UTC)

#xml #java #date #format
star

Tue Mar 22 2022 20:48:17 GMT+0000 (UTC) https://bobbyhadz.com/blog/javascript-get-first-day-of-previous-month#:~:text=To%20get%20the%20first%20day,and%201%20for%20the%20day.

#javascript #date
star

Fri Feb 25 2022 12:24:42 GMT+0000 (UTC) https://stackoverflow.com/questions/136782/convert-from-mysql-datetime-to-another-format-with-php

#php #date #mysql
star

Thu Feb 24 2022 07:51:11 GMT+0000 (UTC)

#ios #swift #day #date #difference
star

Sun Feb 14 2021 13:24:52 GMT+0000 (UTC)

#modx #date #locale
star

Sun Nov 29 2020 12:48:42 GMT+0000 (UTC) https://docs.modx.com/current/en/building-sites/tag-syntax/date-formats

#modx #date #publishedon #format
star

Thu Oct 15 2020 07:34:37 GMT+0000 (UTC) https://madza.hashnode.dev/24-modern-es6-code-snippets-to-solve-practical-js-problems?guid

#javascript #date

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension