YuraB 4 Posted October 6, 2015 Report Share Posted October 6, 2015 I just wanted to share function that I wrote for adding time interval to specific date. It seems that UBOT missing basic date manipulation functions, so I decided to write my own. I hope it will be useful to someone define $date_add(#d, #interval, #ivalue) { if($comparison(#interval,"=","SECONDS")) { then { set(#st,$eval("var d=new Date('{#d}');d.setSeconds(d.getSeconds()+{#ivalue});d;"),"Local") } } if($comparison(#interval,"=","MINUTES")) { then { set(#st,$eval("var d=new Date('{#d}');d.setMinutes(d.getMinutes()+{#ivalue});d;"),"Local") } } if($comparison(#interval,"=","HOURS")) { then { set(#st,$eval("var d=new Date('{#d}');d.setHours(d.getHours()+{#ivalue});d;"),"Local") } } if($comparison(#interval,"=","DAYS")) { then { set(#st,$eval("var d=new Date('{#d}');d.setDate(d.getDate()+{#ivalue});d;"),"Local") } } if($comparison(#interval,"=","MONTHS")) { then { set(#st,$eval("var d=new Date('{#d}');d.setMonth(d.getMonth()+{#ivalue});d;"),"Local") } } set(#MM,$eval("var d1=new Date('{#st}');('0'+(d1.getMonth()+1)).slice(-2);"),"Local") set(#DD,$eval("var d1=new Date('{#st}');('0'+d1.getDate()).slice(-2);"),"Local") set(#YYYY,$eval("var d1=new Date('{#st}');d1.getFullYear();"),"Local") set(#hh,$eval("var d1=new Date('{#st}');('0'+d1.getHours()).slice(-2);"),"Local") set(#mm,$eval("var d1=new Date('{#st}');('0'+d1.getMinutes()).slice(-2);"),"Local") set(#ss,$eval("var d1=new Date('{#st}');('0'+d1.getSeconds()).slice(-2);"),"Local") return("{#MM}/{#DD}/{#YYYY} {#hh}:{#mm}:{#ss}") } Here is how you can use it: alert($date_add($date, "MONTHS", "-2")) alert($date_add($date, "MONTHS", "1")) alert($date_add($date, "SECONDS", "60")) alert($date_add($date, "MINUTES", "5")) alert($date_add($date, "HOURS", "4")) alert($date_add($date, "DAYS", "10")) For example, you can call some function as many times as possible for the next 5 minutes: set(#tmpDate,$date_add($date, "MINUTES", "5"),"Global") loop while($comparison($date,"<",#tmpDate)) { callMyFunction } 3 Quote Link to post Share on other sites
HelloInsomnia 1103 Posted October 6, 2015 Report Share Posted October 6, 2015 Ubot is missing it but there is a plugin for it: http://ubotdev.com/free-plugin-datetime-manipualtion But good job nonetheless Quote Link to post Share on other sites
YuraB 4 Posted October 6, 2015 Author Report Share Posted October 6, 2015 HelloInsomnia, maybe I miss something but the plugin you mentioned does only date formatting.I didn't find any reference to adding time interval to the given date. Please correct me if I'm wrong Quote Link to post Share on other sites
HelloInsomnia 1103 Posted October 6, 2015 Report Share Posted October 6, 2015 Yep you can do time conversions and outputs in pretty much any format you can think of. It's free so give it a try and if you get stuck let me know maybe I'll be able to help you. Quote Link to post Share on other sites
YuraB 4 Posted October 7, 2015 Author Report Share Posted October 7, 2015 HelloInsomnia, that the thing, I don't need date conversion/formatting, all I needed is adding interval functionality. Your plugin does not do that. Quote Link to post Share on other sites
HelloInsomnia 1103 Posted October 7, 2015 Report Share Posted October 7, 2015 It's not my plugin, but anyways I misread what you meant and I see what it does now. I thought you wanted to convert time zones and not manipulate the date/time in the fashion your function does. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.