I'd like to subtract 5 minutes from DateTime.now() May I know how I can do this?
Thanks!
addMinutes()
It has add in it's name but nobody says you can't pass a negative number.
DateTime dt = System.now();
DateTime earlier = dt.addMinutes(-5);
DateTime fullHour = dt.addMinutes( -dt.minute() ).addSeconds( -dt.second() );
System.debug(dt + '\t' + earlier + '\t' + fullHour);
2014-01-11 08:40:13 2014-01-11 08:35:13 2014-01-11 08:00:00
Just subtract time in days (Double format) from DateTime.now() as follows:
i.e. 5 (minutes) = 5 / 1440 (days)
DateTime dt = DateTime.now() - Double.valueOf(5) / 1440;
Related
I have a string that represents time duration of 54:34:41 i.e. 54 hours, 34 minutes, 41 seconds.
I would like to extract the 54 hours and subtract it from the current system time.
However when I run below I get java.time.format.DateTimeParseException: Text '54:34:41' could not be parsed: Invalid value for HourOfDay (valid values 0 - 23): 54
How can I extract 54 hours and subtract from current time?
private val formatterForTime: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
val timeDuration = formatterForTime.parse("54:34:41")
val currentTime = LocalDateTime.now()
val newTime = currentTime.minusHours(timeDuration.get(ChronoField.HOUR_OF_DAY).toLong())
tl;dr
ZonedDateTime
.now(
ZoneId.of( "Asia/Tokyo" )
)
.minusHours(
Integer.parseInt( "54:34:41".split( ":" )[0] )
)
Details
Parse hours
Get the number of hours.
int hours = Integer.parseInt( "54:34:41".split( ":" )[0] ) ;
ISO 8601
Your input text for a span-of-time does not comply with the ISO 8601 standard for date-time values. The java.time classes by default use the standard formats when parsing/generating text.
If instead of 54:34:41 you had PT54H34M41S, then we could use:
int hours = Duration.parse( "PT54H34M41S" ).toHours() ;
I recommend you stick with the standard format rather than the ambiguous clock-time format.
Capture current moment
Capture the current moment as seen in a particular time zone.
ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Subtract hours
Subtract your hours.
ZonedDateTime earlier = zdt.minusHours( hours ) )
I would like to specify a number that specifies the day of week and then have ASP get the upcoming date for that week day specified.
Example:
Dim xWeekDay
xWeekDay=1 ' <-- 1 would be a Monday...and Sunday would be 7
Dim NextDdate
NextDdate= ???? <-- I want to calculate and show the Upcoming Date here
So the above line would look like this when it's populated.
NextDdate=7/1/2013
Try this:
today = Weekday(Date, vbMonday)
If xWeekDay > today Then
NextDate = Date + (xWeekDay - today)
Else
NextDate = Date + (xWeekDay + 7 - today)
End If
Weekday(Date, vbMonday) is the number of the currend day of the week (with Monday being set as the first weekday). If xWeekDay is in the future (xWeekDay > today), then the next occurrence is xWeekDay - today days away. Otherwise it's xWeekDay + 7 - today days away. Add that difference to the current date and you have the date you're looking for.
I am trying to convert 24 hour Time values to minutes
From:
(TIME)
17:00:00
16:55:00
17:30:00
To:
(NUM)
1060
993
1038
Currently I am multiplying the time values by 60 17*60, 16.55*60, 17.30*60
How would I accomplish this? am I doing it right? and what am I doing wrong?
SAS time values are stored as seconds. If it's truly a 'time' value, anyway, and not a character string. Thus, you can DIVIDE by 60, rather than multiplying.
data want;
input timeval TIME9.;
minutes=timeval/60;
format minutes BEST12.;
format timeval TIME9.;
put timeval= minutes=;
datalines;
17:00:00
16:55:00
17:30:00
;;;;
run;
If it's not stored as a time value (numeric) but as a string, you need to INPUT(timeval,TIME9.) in order to do that; so
minutes = input(timeval,TIME9.)/60;
would work.
No idea what this has to do with SQL, but in general, assuming that hours is 0-23 rather than 0-12 with an am/pm indicator, this pseudocode
( 60.0 * hours ) // convert 0-23 hours to minutes
+ minutes // minutes don't need conversion
+ ( seconds / 60.0 ) // convert 0-59 seconds to fractional minute
should give you the offset from start of day (midnight, 00:00:00) as a fractional number of minutes. If you've got an am/pm indicator, you need to factor that in, something like this:
( 60.0 * ( hours + ( isPM ? 12 : 0 ) ) ) // convert 0-23 hours to minutes
+ minutes // minutes don't need conversion
+ ( seconds / 60.0 ) // convert 0-59 seconds to fractional minutes
Working on a website that's a combination of Jquery Mobile and ASP 4. Currently I'm stuck on a form I'm trying to get to do a SQL insert. I'm trying to use code behind in VB to handle my insert...
All that to say: I need my page to convert a minute value into hour&quarter hour format, rounding to nearest quarter hour.
Currently I'm taking two times entered into text boxes (ex. 8:00:00 AM and 9:12:00 AM). If I use a TimeSpan I can calculate the difference between the two values and dump that difference into a variable, for example tElapsed = tSpan.TotalMinutes.ToString would set tElapsed = 72 for the above times. I need to convert this to a 1.0hr, 1.25hr, 1.5hr, etc format and round 3+ minutes to next half hour... and I keep getting stuck.
Synopsis:
Have:
Text Box: Time1 = 8:00:00 AM
Text Box: Time2 = 9:12:00 AM
Dim tDiff As String = DateTime.Parse.(Time2.Text) - DateTime.Parse(Time1.Text)
Dim tElapse As String = tDiff.TotalMinutes.ToString
tElapse will return 72 for the above, now I need to convert 72 (minutes) to 1.25 (hours).
1.25 hours can be any of: 64 minutes - 78 minutes
Any help would be appreciated...
You can use Math.DivRem
int remainder;
int whole = Math.DivRem(tDiff.TotalMinutes, 60, out remainder);
At this point remainder will now be what's left between hours. 0 to 59 minutes. I wasn't clearn on how you wanted exactly to round that, but (double)remainder / 60.00 will give you the decimal place, which you can then add to the whole.
Here is the solution I used, posting it as an answer so the code structure will show up:
Dim tDiff As TimeSpan = DateTime.Parse(textboxEnd.Text) - DateTime.Parse(textboxStart.Text)
Dim mins As Integer = tDiff.TotalMinutes.ToString
Dim remainder As Integer
Dim hrs As String = Math.DivRem(mins, 60, remainder)
Dim qtyorder As String
Select Case remainder
Case 1 To 2
qtyorder = hrs
Case 3 To 18
qtyorder = hrs & ".25"
Case 19 To 28
qtyorder = hrs & ".50"
Case 29 To 48
qtyorder = hrs & ".75"
Case Else
qtyorder = (hrs + 1) & ".00"
End Select
lblResult.Text = qtyorder
I want to get the Sunday & Saturday of the week from which a date is provided.
I have access to the following functions only:
getDate() returns a number from 0-6 (0 being sunday)
getDay() returns a number from 1-31
getMonth() returns a number from 0-11
getFullYear() returns the current year
I am doing this on titanium.
Per your description above, I came up with:
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));
If I follow the MDN doc, I come up with (works in Ti too):
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDay() + input.getDate());
var sun = new Date(input.getFullYear(), input.getMonth(), input.getDate() + (input.getDay() - 6));
Where input is a javascript Date object.
The date object will take care or changing the month and/or year if necessary.
Hope this helps.