Jump to content

moment.fm [ date format ]


rivet
 Share

Recommended Posts

I am working on a custom function to mimic some of moment.js tokens.
If you have a moment, please let me know if you can find any bugs of efficiencies

thanks.

 

/*
MOMENT.FM ( dateTimeStamp ; formatString ; relativeDate )

reformat date, following moment.js tokens


Token	Output
===============
Month	
M		1 2 ... 11 12
Mo		1st 2nd ... 11th 12th
MM		01 02 ... 11 12
MMM		Jan Feb ... Nov Dec
MMMM	January February ... November December

Quarter
Q		1 2 3 4
Qo		1st 2nd 3rd 4th

Day of Month
D		1 2 ... 30 31
Do		1st 2nd ... 30th 31st
DD		01 02 ... 30 31

Day of Year
DDD		1 2 ... 364 365
DDDo	1st 2nd ... 364th 365th
DDDD	001 002 ... 364 365

Day of Week
d		0 1 ... 5 6
do		0th 1st ... 5th 6th
dd		Su Mo ... Fr Sa
ddd		Sun Mon ... Fri Sat
dddd	Sunday Monday ... Friday Saturday

Week of Year
w		1 2 ... 52 53
wo		1st 2nd ... 52nd 53rd
ww		01 02 ... 52 53


//based on
momentjs — https://momentjs.com/docs/#/displaying/
Kyle Williams — https://www.briandunning.com/cf/2522
Vaughan Bromfield — https://www.briandunning.com/cf/737


version: 0.1
author: david schellenberg 2021


*/




Let ( [
	
	//PAYLOAD
	ts  = Timestamp ( Date ( 10 ; 13 ; 2021 ) ; Time ( 14 ; 4 ; 3 ) ) ;
	//ts  = Date ( 10 ; 13 ; 2021 ) ;
	//ts  = Time ( 6 ; 4 ; 3 ) ;
 	
	rel = true ; //relative date
	rel = false ;

 	//fmt = "ddd MMMM DD YYYY, hh:mmAA" ;
 	//fmt = "dddd, MMMM Do YYYY, h:mm:ss a CC" ;
 	//fmt = "ddd, hA" ;
 	//fmt = "H:M:s - HH:MM:ss | h:m:s - hh:mm:ssA" ;
 	fmt = "ddd MMM Do YYYY, h:mm:ss a" ;
 	//fmt = "ddd MMM Do YYYY, HHmm" ;



	//CALC
	valid = IsValid ( ts ) ;
	ts  = GetAsTimestamp ( ts ) ;


	dte = Date ( Month (ts) ; Day (ts) ; Year (ts) ) ;
	dt  = Case ( GetAsText ( dte ) = "" ; Get ( CurrentDate )  ; dte ) ;

	tme = Time ( Hour (ts) ; Minute (ts) ; Seconds (ts) ) ;
	tm  = Case ( GetAsText ( tme ) = "" ; Get ( CurrentTime )  ; tme ) ;

	
	
	//RELATIVE
	yy = Year ( dt ) ;
	cd = Get ( CurrentDate ) ;
	cy = Year ( cd ) ;
	ry = Case ( yy = cy ; "" ; " "& yy );

	
	rdate =
	Case ( 

		rel = False ;  
		"format" ;
	
		dt = cd + 0 ; "Today" ;
		dt = cd + 1 ; "Tomorrow" ;
		dt = cd - 1 ; "Yesterday" ;
		dt >=cd and dt < cd + 8 ; DayName ( dt ); // 1 week ahead

		"format" 

	 );



	//DATE 
	dname = Substitute ( 

				DayName ( dt )  ;  
				[ "a" ; "å" ]; [ "d" ; "∂" ]; [ "h" ; "˙" ]; [ "m" ; "µ" ]; [ "s" ; "ß" ]; [ "w" ; "∑" ];  
				[ "A" ; "Å" ]; [ "D" ; "Î" ]; [ "H" ; "Ó" ]; [ "M" ; "Â" ]; [ "S" ; "Í" ]; [ "W" ; "Œ" ]

			) ;

	mname = Substitute ( 

				MonthName ( dt )  ;  
				[ "a" ; "å" ]; [ "d" ; "∂" ]; [ "h" ; "˙" ]; [ "m" ; "µ" ]; [ "s" ; "ß" ]; [ "w" ; "∑" ];  
				[ "A" ; "Å" ]; [ "D" ; "Î" ]; [ "H" ; "Ó" ]; [ "M" ; "Â" ]; [ "S" ; "Í" ]; [ "W" ; "Œ" ]

			) ;


	dw  = Int( GetAsNumber( DayOfWeek ( dt ) ) ) ;
	dwn = Mod ( Abs ( dw ) ; 100 ) ;
	dwo = dw & Choose ( Min( 4 ; Mod( dwn ; 10 ) ) * ( ( dwn < 11 ) or ( dwn > 13 ) ) ; "¡" ; "™" ; "£" ; "¢" ; "∞" ) ;

	dm  = Int( GetAsNumber( Day ( dt ) ) ) ;
	dmn = Mod ( Abs ( dm ) ; 100 ) ;
	dmo = dm & Choose ( Min( 4 ; Mod( dmn ; 10 ) ) * ( ( dmn < 11 ) or ( dmn > 13 ) ) ; "¡" ; "™" ; "£" ; "¢" ; "∞" ) ;

	dy  = Int( GetAsNumber( DayOfYear ( dt ) ) ) ;
	dyn = Mod ( Abs ( dy ) ; 100 ) ;
	dyo = dy & Choose ( Min( 4 ; Mod( dyn ; 10 ) ) * ( ( dyn < 11 ) or ( dyn > 13 ) ) ; "¡" ; "™" ; "£" ; "¢" ; "∞" ) ;

	m  = Int( GetAsNumber( Month ( dt ) ) ) ;
	mn = Mod ( Abs ( m ) ; 100 ) ;
	mo = m & Choose ( Min( 4 ; Mod( mn ; 10 ) ) * ( ( mn < 11 ) or ( mn > 13 ) ) ; "¡" ; "™" ; "£" ; "¢" ; "∞" ) ;

	q  = Div ( Mod ( Month ( dt ) - 0 ; 12 ) ; 3 ) + 1 ;
	qn = Mod ( Abs ( q ) ; 100 ) ;
	qo = q & Choose ( Min( 4 ; Mod( qn ; 10 ) ) * ( ( qn < 11 ) or ( qn > 13 ) ) ; "¡" ; "™" ; "£" ; "¢" ; "∞" ) ;

	w  = Int( GetAsNumber( WeekOfYear ( dt ) ) ) ;
	wn = Mod ( Abs ( w ) ; 100 ) ;
	wo = w & Choose ( Min( 4 ; Mod( wn ; 10 ) ) * ( ( wn < 11 ) or ( wn > 13 ) ) ; "¡" ; "™" ; "£" ; "¢" ; "∞" ) ;



	//TIME
	hr  = Hour ( tm ) ;
	pm  = hr - 12 > 0 ;
	ap  = Case ( pm ; "pµ" ; "aµ" ) ;
	apU  = Case ( pm ; "PÂ" ; "AÂ" ) ;

	hh  = Case ( pm ; hr - 12 ; hr ) ;



	//RESULT
	result = 
	Case ( 

		valid <> 1 ;
		"Invalid date" ;

		rdate <> "format" ; 
		rDate; 

		Substitute ( fmt ;

			//Year
			[ "YYYY";		Year ( dt ) ] ;
			[ "YY";			Right(Year ( dt ) ; 2 ) ] ;
			[ "Qo";			qo ] ;
			[ "Q";			q ] ;

			//Month
			[ "Mo";			mo  ] ;
			[ "MMMM";		mname ] ;
			[ "MMM";		Left ( mname ; 3 ) ] ;
			[ "MM";			Right ( "00" & Month ( dt ) ; 2 ) ] ;
			[ "M";			Month ( dt ) ] ;

			//Day of Year
			[ "DDDo";		dyo ] ;
			[ "DDDD";		Right ( "000" & DayOfYear ( dte ) ; 3 ) ] ;
			[ "DDD";		DayOfYear ( dte ) ] ;

			//Day of Month
			[ "Do";			dmo  ] ;
			[ "DD";			Right ( "00" & Day ( dte ) ; 2 ) ] ;
			[ "D";			Day ( dte ) ] ;

			//Day of Week
			[ "do";			dwo  ] ;
			[ "dddd";		dname ] ;
			[ "ddd";		Left ( dname ; 3 ) ] ;
			[ "dd";			Left ( dname ; 2 ) ] ;
			[ "d";			DayOfWeek ( dte ) ] ;
		
			//Week of Year
			[ "wo";			wo  ] ;
			[ "ww";			Right ( "00" & WeekOfYear ( dte ) ; 2 ) ] ;
			[ "w";			WeekOfYear ( dte ) ] ;

			//Century
			[ "CC";			Left ( Year ( dt ) ; 2 ) +1 ] ; // Century

			//TIME
			[ "HH";			Right ( "00" & Hour ( tm ) ; 2 ) ] ;  //24
			[ "H";			Hour ( tm ) ] ;

			[ "hh";			Right ( "00" & hh ; 2 ) ] ;
			[ "h";			hh ] ;

			[ "mm";			Right ( "00" & Minute ( tm ) ; 2 ) ] ;
			[ "m";			Minute ( tm ) ] ;

			[ "ss";			Right ( "00" & Seconds ( tm ) ; 2 ) ] ;
			[ "s";			Seconds ( tm ) ] ;

			[ "A";			apU ] ;
			[ "a";			ap ] ;


			//ordinal
			[ "¡" ; "th" ]; [ "™" ; "st" ]; [ "£" ; "nd" ]; [ "¢" ; "rd" ]; [ "∞" ; "th" ]; 

			//name
			[ "å" ; "a" ]; [ "∂" ; "d" ]; [ "˙" ; "h" ]; [ "µ" ; "m" ]; [ "ß" ; "s" ]; [ "∑" ; "w" ]; [ "œ" ; "q" ];
			[ "Å" ; "A" ]; [ "Î" ; "D" ]; [ "Ó" ; "H" ]; [ "Â" ; "M" ]; [ "Í" ; "S" ]; [ "Œ" ; "W" ]; [ "Œ" ; "Q" ]



		)

	)


	] ;



	result 


)

 

Edited by rivet
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.