I am building my first ReactNative iOS and Android app. I am an iOS coder with Swift and Obj-C. How do I fetch the current date using ReactNative.
Shall I use Native Modules or is there an ReactNative API from which I can get this data ?
The JavaScript runtime in React Native has access to date information just like any other environment. As such, simply:
new Date()
... will give you the current date. Here's the MDN on working with dates in JS.
If you want a good JS library to make working with dates even easier, check out MomentJS.
I used {new Date()} was generating Invariant Violation: Objects are not valid error the following date function worked for me.
const getCurrentDate=()=>{
var date = new Date().getDate();
var month = new Date().getMonth() + 1;
var year = new Date().getFullYear();
//Alert.alert(date + '-' + month + '-' + year);
// You can turn it in to your desired format
return date + '-' + month + '-' + year;//format: d-m-y;
}
For more info https://reactnativecode.com/get-current-date-react-native-android-ios-example
It works for ios as well.
I think this should work;
new Date().toLocaleString()
It will return date and time formatted in your local format (usually in the below format);
"6/22/2021, 2:59:00 PM"
if you get currentmilisec date use + new Date() and if get current use new Date()
I hope Help to you
GoodLuk
Related
I know how to get the month of current page but I want to know how to get the month of current page in string in fscalender please help me I'm new to iOS.
let values = Calendar.current.dateComponents([Calendar.Component.month, Calendar.Component.year], from: self.fscalenderobj.currentPage)
var CURRENT_MONTH = values.month
it's in integer form like in today month -> 5 ,I want it in name like May.
It's pretty simple.
let monthSymbols = Calendar.current.shortMonthSymbols (or monthSymbols dependeing upon what you need)
You can refer this
https://developer.apple.com/documentation/foundation/calendar/2293753-shortmonthsymbols
I want to convert this string "2018-11-13T9:24" to Date in my js Code
var date = new Date('2018-11-13T9:24');
app.dialog.alert(date, 'Date');
but in my alert, I see:
I don't have any problem with time after 10 like '2018-11-13T11:24'
I have been trying to setup local notifications with Titanium mobile for iOS, using a custom date time, but its not working with custom date time. What I am trying is
var notification = Ti.App.iOS.scheduleLocalNotification({
alertBody:"Dummy text",
alertAction:"Re-Launch!",
userInfo:{"hello":"world"},
date: new Date("2015-03-22 01:45")
});
However when I use this for date, It works.
date: new Date(new Date.getTime()+8000)//Current date time + 8 secs after.
What should I do to make this work.
date: new Date("2015-03-22 01:45")
Thanks.
The date string you are passing to the constructor is not valid.
You could use something like:
date: new Date("1/20/2015 00:00:00")
Where the format is MM/DD/YYYY HH:MM:SS
I'm trying get current date in zend 2 , I use this code to get current date and time
$datetime = new DateTime();
But it throw an error "Class 'Application\Controller\DateTime' not found in C:\wamp\www\zf2\module\Application\src\Application\Controller\IndexController.php on line 26
how can I fix this bug? thank you so much :)
$datetime = new \DateTime("now");
or if you dont like the backslash include
use DateTime;
at the start of your file and then the format is;
$datetime = new DateTime("now");
Using Modx evo, I am trying to use the following snippet to display the date difference between published date and current date (in days), but getting weird output. What am I doing wrong?
<?php
$date2=$modx->documentObject['createdon'];
$date1=time();
$dateDiff = $date1 - $date2;
$daysOld = floor($dateDiff/(60*60*24));
return $daysOld;
?>
The thing is that the date is stored as a SQL-date, not a timestamp.
Read the docs: http://rtfm.modx.com/display/revolution20/Date+Formats
So, this should work:
$dateDiff = $time() - strtotime($modx->documentObject['createdon']);
$daysOld = floor($dateDiff/(60*60*24));
return $daysOld;