MomentJS date string adds one day - date

I don't understand why this date is saved as +1 day:
startdate = "2017-11-29T23:59:59.999Z";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives 30/11/2017
But if I do:
startdate = "2017-11-29";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives the correct date 29/11/2017
Any ideas?
Here is a jsfiddle showing this: http://jsfiddle.net/jbgUt/416/
Thanks!

If a time part is included, an offset from UTC can also be included as +-HH:mm, +-HHmm, +-HH or Z.
Add utc() to avoid it.
moment(startdate).utc().format('DD-MM-YYYY')
or
moment.utc(startdate).format('DD-MM-YYYY')
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()

Late to the party on this one, but I did just convert a few of our product's date-time objects to https://moment.github.io/luxon/
Takes out the need for the .utc() method above.

Related

flutter datetime how to convert it

I have the following datetime from a database: 2022-10-02T23:10:24.736Z I want to compare it to a datetime now and get the difference in second. Basically I have the code for it, but im not able to convert the datetime.now to the mentioned format. It tells me it has a difference of 122 minutes but it should only be 2 minutes..
DateTime datenow = DateTime.now();
DateTime dateFormDatabase = DateTime.parse(widget.data[widget.currIndex-1]["roundEnd"]);
print(datenow); // <---- 2022-10-01 23:22:45.522687
print(dateFormDatabase); // <--- 2022-10-01 23:25:24.736Z
var diff = dateFormDatabase.difference(datenow);
print(diff.inMinutes); // <--- 122 but it should be 2minutes
The returned difference is correct. You are probably looking at different timezones here. DateTime.now() returns the current date and time in your local timezone. The parsed date from your database seems to be coming in as UTC. Notice the "Z" at the end here: 2022-10-01 23:25:24.736Z.
If you want to visually compare them you could convert either of those to UTC/local with toUtc or toLocal

Converting timestamp object to date with difference in flutter

Let’s say I have an object of type Timestamp set to 2 days from now.
How to get the difference in time and then format it in DateFormat('hh : mm : ss') in dart ?
Construct a DateTime from the Timestamp.
Subtract from that DateTime the initial time (which I presume should be DateTime.now()) to get a Duration.
Format the Duration. Its default .toString() implementation uses hh:mm:ss.microseconds, which is close to what you want. If you want more control, you can easily format it yourself.
var dateTime = DateTime.fromMicrosecondsSinceEpoch(timestamp.timestamp!);
var duration = dateTime.difference(DateTime.now());
print(duration);

Convert mach absolute timestamp to NSDate

I have a mach absolute timestamp from a sql database, I want to read in my app. I want to display this timestamp, so I want to convert it to NSDate. How is this possible? I have a timestamp like this: 443959550 (result in this case is UTC: 26.01.2015 10:05:50)
443959550 seems to be a time interval since the reference date
1 January 2001, GMT:
let date = NSDate(timeIntervalSinceReferenceDate: 443959550)
println(date) // 2015-01-26 10:05:50 +0000
Take a look at timeIntervalSince1970. There you can set a timestamp as init-parameter.
var timeStamp = 443959550
//NSDate
var date = NSDate(timeIntervalSince1970:443959550)

How do I make a date representation ('1/1/2014') from = 3-1-2014 5:50:46, Function loadDatabaseFromSheet()

I am new to Appsript Script.Db
We have a spreadsheet whit about 300 row's of date in the first column date : 3-1-2014 5:50:46.
When we do a function loadDatabaseFromSheet() we get the error date Timestamp is not a number.
ScriptDB cannot store Date objects directly; instead, you must store a representation of the >date and reconstruct it later. If you don't intend to search based on dates, then you can >store the numeric timestamp from the Date object like this:
var date = new Date('1/1/2014');
var item = {
timestamp: date.getTime();
}
var record = db.save(item);
Do we have change all the date by hand in a proper way from 3-1-2014 5:50:46 to '3/1/2014'?
Hope that there is a better way to get this done?
// How and where can i make a representation: ('1/1/2014'); I hope i don't have to change
// all the date's in the spreadsheet by hand ?My spreadsheet has 300 Row's, first collumn =
The script DB cannot save Date datatype by default, so what you need to do is covert it to its time representation and save it to DB as the above info suggests.
As for your question regarding the need to change date format, it will not be necessary just do a (new Date(value)).getTime() at the time of saving to the DB. Where value represents the datetime in the first column.

How to make date comparison in yii framework

I am creating project in yii. I am having Poll table with the fields as-
-pollId
-pollQustion
-isActive
-publishDate
I want to check weather publishdate of poll is not greater than current date.
I am implementing as-
$CurrrentDate=new CDbExpression('NOW()');
if($record->publishDate < $CurrrentDate))
{
some code......
}
But its not executing correctly. Code is executing even if publish date is greater than current date. So how to make this comparison in yii framework. please help me
PHP do not have a date format. When you compare $CurrentDate and $record->publishDate you compare strings. Well, for right compare you need to convert it to Unix's timestamp
// here you set right date/time pattern
// You need to explain CDateTimeParser for your pattern
// just easy use `strtotime` if you have 2012-12-12 12:12:12
$publishTimestamp = strtotime($record->publishDate);
// $publishTimestamp = CDateTimeParser::parse($record->publishDate, 'yyyy-MM-dd hh:mm:ss');
// if you need to use Hours and minutes just easy use `time()`
$currentDateTimestamp = time();
// $currentDateTimestamp = strtotime(date('Y-m-d H:i:s'));
// now we can compare
if ($publishTimestamp < $currentDateTimestamp) {
// do some
}