Lua How to reformat a date? - string

I am currently retrieving a date in the format of 2020-09-23T09:03:46.242Z (YYYY-MM-DDThh:mm:ss.sssZ) and I am trying to convert it into Wed Sep 23 09:03:46 2020. Struggling with the string manipulations, does anyone have any ideas?
Essentially my goal is to be able to perform os.time() on the date, but im aware I may need to do some reformatting beforehand.
Any help is greatly appreciated
Thanks, Scott.

local s = '2020-09-23T09:03:46.242Z'
local t = {}
t.year, t.month, t.day, t.hour, t.min, t.sec =
assert(s:match'^(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)')
print(os.date('%c', os.time(t)))

Try this:
local function convert (s)
local source_format = '(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)%.'
local year, month, day, hour, min, sec = string.match( s, source_format )
local unix_time = os.time {
year = tonumber(year),
month = tonumber(month),
day = tonumber(day),
hour = tonumber(hour),
min = tonumber(min),
sec = tonumber(sec)
}
local target_format = '%a %b %d %H:%M:%S %Y'
return os.date( target_format, unix_time )
end

Related

How to remove milliseconds in datetime python?

I want to print the amount of days, hours, minutes and seconds until christmas day but when it prints, it also gives me the millisecond. I tried doing print(remain_until_xmas.strip(.) but that didn't work. Here is the code
import datetime as dt
xmas = dt.datetime(2020, 12, 25)
now = dt.datetime.now()
until_xmas = xmas - now
print(until_xmas)
To get the time live, you can use this code:
import time
time = str(time.strftime("%M")) + ":" + str(time.strftime("%S"))
print(time)
This will give you the current time in Minutes and Seconds, and if you put it in a loop, it will keep updating itself.
If you want the day and month, you can use
print(time.strftime("%A, %B %e")) -
With this code, you can then subtract the Xmas date from the current date, and retrieve what you want.
This would be your final code:
import time
month = time.strftime("%m")
day = time.strftime("%d")
hour = time.strftime("%H")
minute = time.strftime("%M")
second = time.strftime("%S")
chrmonth = 12
chrday = 23
chrhour = 12
chrminute = 60
chrsecond = 60
print("The time until christmas is, ", int(chrmonth) - int(month), "months", int(chrday) - int(day), "days", int(chrhour) - int(hour), "hours", int(chrminute) - int(minute), "minutes", int(chrsecond) - int(second), "seconds")
Hopefully this helps!
you can use .strftime()
print(until_xmas.strftime("%m/%d/%Y, %H:%M:%S"))
see more here about strftime.

I need to find how many years between the date? python 3

I need to find how many years between the date?
Input
dat = ['Apr 2010', 'Dec 2012']
output like this
{
total_years = 2.8
}
Not sure if this is the best way to do it but it'll work
import datetime
start = datetime.datetime.strptime("Apr 2010", "%b %Y")
end = datetime.datetime.strptime("Dec 2012", "%b %Y")
diff = (end - start).days // 365

Date Time difference between hour of the day and time now

Time = datetime.datetime.now().time()
I wanted to find the difference in seconds between the Time above^ and the hour of the day:
for example if the Time = 15:07:25.097519, so the hour of the day is 15:00:00, i want to store 445.097519‬ seconds to a variable.
How can I do that?
I am an amateur at this please help!!
import datetime
now = datetime.datetime.now()
hour = now.replace(minute=0, second=0, microsecond=0)
seconds = (now - hour).seconds + (now - hour).microseconds / 1000000
Here is one way to do it. Extract the hour from current time, and initialize a new datetime.time object with hour as input. Then you convert both of the timestamps to datetime and then do the subtraction. Code below does that
Time = datetime.datetime.now().time()
hour = str(Time).split(":")[0]
currentHourTime = datetime.datetime(2019,10,31,int(hour),0,0,0).time()
dateTimeCurr = datetime.datetime.combine(datetime.date.today(), Time)
dateTimeCurrHour = datetime.datetime.combine(datetime.date.today(), currentHourTime)
dateTimeDifference = dateTimeCurr - dateTimeCurrHour
dateTimeDifferenceInSeconds = dateTimeDifference.total_seconds()
print(dateTimeDifferenceInSeconds)

Add days to current date in groovy

I am trying to get the current date and change the format and add 30 days to it, i tried the following in groovy :
def date = new Date().format("yyyy-MM-dd")
def laterdate = date + 30
log.info laterdate
I get the output as (formatting looks good)
Mon Jul 24 12:24:04 MST 2017:INFO:2017-07-2430
can someone please advise where i am doing wrong
To add days:
Date date = new Date().plus(30)
To Substract days:
Date date = new Date().plus(-30)
def today = new Date()
def yesterday = today + 30
log.info today.format("yyyy-MM-dd")
log.info yesterday.format("yyyy-MM-dd")

How to get next minute of the time?

def waktusekarang
SimpleDateFormat abc = new SimpleDateFormat("HHmm")
waktusekarang = abc.format(new Date())
if current time is 13:58, then it will be become 1358 because (SimpleDateFormat is HHmm). then how to +2 minutes to the current time then the result become 1400.
i try new Date()+2 but its not success and the result become 13581,and i also try to parseInteger but its not success too.
please help my problem , how can i +1 or +2 minutes to current time if current minute is 59.because if minute is 59 then +1 will be 60 ,it must be 00
Use can use Calendar to add minutes to your time, appropriately your time would be incremented:
SimpleDateFormat abc = new SimpleDateFormat("HHmm");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 2);
String waktusekarang = abc.format(cal.getTime());
System.out.println(waktusekarang);
A Groovy solution would be:
Date in2mins = use( groovy.time.TimeCategory ) {
new Date() + 2.minutes
}
String waktusekarang = in2mins.format( 'HHmm' )
println waktusekarang
Dont use format(Date). Use format(int) and then call System.currentTimeMillis() and add 120000 ms = 2 mins. Thats a way to do it without calendar. But you should think of using calendar.
SimpleDateFormat abc = new SimpleDateFormat("HHmm");
waktusekarang = abc.format(System.currentTimeMillis()+120000);

Resources