How do I add two weeks to Time.now?
You don't have such nice helpers in plain Ruby. You can add seconds:
Time.now + (2*7*24*60*60)
But, fortunately, there are many date helper libraries out there (or build your own ;) )
Adding two weeks php
You made a mistake on line 4 by putting a number variable as a first parameter of strtotime
. strtotime
expects a string of a valid date/time format as a first parameter otherwise it returns FALSE.
How I think your code should be:
$twoweeks = strtotime($time_db);
$date = strtotime("+ 2 weeks", $twoweeks);
echo date('d/m/y', $date);
Or maybe even:
echo date('d/m/y', strtotime($time_db . ' + 2 weeks'));
Add 1 week to current date
You want to leave it as a DateTime
until you are ready to convert it to a string.
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
Go setting time ie. 2 weeks, 1 month, 1 year, etc
Use AddDate
twoWeeksFromNow := time.Now().AddDate(0, 0, 14)
oneMonthFromNow := time.Now().AddDate(0, 1, 0)
oneYearFromNow := time.Now().AddDate(1, 0, 0)
Javascript Date Plus 2 Weeks (14 days)
12096e5
is a magic number which is 14 days in milliseconds.
var fortnightAway = new Date(Date.now() + 12096e5);
jsFiddle.
How to add weeks to date using javascript?
You can do this :
const numWeeks = 2;
const now = new Date();
now.setDate(now.getDate() + numWeeks * 7);
or as a function
const addWeeksToDate = (dateObj,numberOfWeeks) => {
dateObj.setDate(dateObj.getDate()+ numberOfWeeks * 7);
return dateObj;
}
const numberOfWeeks = 2
console.log(addWeeksToDate(new Date(), 2).toISOString());
You can see the fiddle here.
According to the documentation in MDN
The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
How do I add 2 weeks to a Date in java?
Use Calendar and set the current time then user the add method of the calendar
try this:
int noOfDays = 14; //i.e two weeks
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOfOrder);
calendar.add(Calendar.DAY_OF_YEAR, noOfDays);
Date date = calendar.getTime();
Are adding weeks, months or years to a date/time independent from time zone?
The answer may surprise you but it is NO. You cannot add days, weeks, months, or years to a UTC timestamp, convert it to a local time zone, and expect to have the same result as if you had added directly to the local time.
The reason is that not all local days have 24 hours. Depending on the time zone, the rules for that zone, and whether DST is transitioning in the period in question, some "days" may have 23, 23.5, 24, 24.5 or 25 hours. (If you are trying to be precise, then instead use the term "standard days" to indicate you mean exactly 24 hours.)
As an example, first set your computer to one of the USA time zones that changes for DST, such as Pacific Time or Eastern Time. Then run these examples:
This one covers the 2013 "spring-forward" transition:
DateTime local1 = new DateTime(2013, 3, 10, 0, 0, 0, DateTimeKind.Local);
DateTime local2 = local1.AddDays(1);
DateTime utc1 = local1.ToUniversalTime();
DateTime utc2 = utc1.AddDays(1);
DateTime local3 = utc2.ToLocalTime();
Debug.WriteLine(local2); // 3/11/2013 12:00:00 AM
Debug.WriteLine(local3); // 3/11/2013 1:00:00 AM
And this one covers the 2013 "fall-back" transition:
DateTime local1 = new DateTime(2013, 11, 3, 0, 0, 0, DateTimeKind.Local);
DateTime local2 = local1.AddDays(1);
DateTime utc1 = local1.ToUniversalTime();
DateTime utc2 = utc1.AddDays(1);
DateTime local3 = utc2.ToLocalTime();
Debug.WriteLine(local2); // 11/4/2013 12:00:00 AM
Debug.WriteLine(local3); // 11/3/2013 11:00:00 PM
As you can see in both examples - the result was an hour off, one direction or the other.
A couple of other points:
- There is no
AddWeeks
method. Multiply by 7 and add days instead. - There is no
ToUtcTime
method. I think you were looking forToUniversalTime
. - Don't call
DateTime.Now.ToUniversalTime()
. That is redundant since inside.Now
it has to take the UTC time and convert to local time anyway. Instead, useDateTime.UtcNow
. - If this code is running on a server, you shouldn't be calling
.Now
or.ToLocalTime
or ever working withDateTime
that has aLocal
kind. If you do, then you are introducing the time zone of the server - not of the user. If your users are not in the same time zone, or if you ever deploy your application somewhere else, you will have problems. - If you want to avoid these kind of problems, then look into NodaTime. It's API will prevent you from making common mistakes.
Here's what you should be doing instead:
// on the client
DateTime local = new DateTime(2013, 3, 10, 0, 0, 0, DateTimeKind.Local);
DateTime utc = local.ToUniversalTime();
string zoneId = TimeZoneInfo.Local.Id;
// send both utc time and zone to the server
// ...
// on the server
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
DateTime theirTime = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi);
DateTime newDate = theirTime.AddDays(1);
Debug.WriteLine(newDate); // 3/11/2013 12:00:00 AM
And just for good measure, here is how it would look if you used Noda Time instead:
// on the client
LocalDateTime local = new LocalDateTime(2013, 3, 10, 0, 0, 0);
DateTimeZone zone = DateTimeZoneProviders.Tzdb.GetSystemDefault();
ZonedDateTime zdt = local.InZoneStrictly(zone);
// send zdt to server
// ...
// on the server
LocalDateTime newDate = zdt.LocalDateTime.PlusDays(1);
Debug.WriteLine(newDate); // 3/11/2013 12:00:00 AM
Related Topics
Ruby: Explicit Scoping on a Class Definition
Ruby Methods That Either Yield or Return Enumerator
Better Way to Turn a Ruby Class into a Module Than Using Refinements
Is Ruby a Scripting Language or an Interpreted Language
How to Remove Validation Using Instance_Eval Clause in Rails
Ruby Classes: Initialize Self VS. @Variable
Code to Generate Gaussian (Normally Distributed) Random Numbers in Ruby
Cool Tricks and Expressive Snippets with Ruby Collections/Enumerables
What Are the Main Differences Between Sinatra and Ramaze
Don't Create View Folder on Rails Generate Controller
Sorting a Two-Dimensional Array by Second Value
Passing Param Values to Redirect_To as Querystring in Rails
MAC Os X Mountain Lion "Rails Is Not Currently Installed on This System."