Would like to convert date time value as following format
2017-04-06T13:53
Thank you
// Exactly your time format
console.log( moment().format( 'YYYY-MM-DDTHH:mm' ) );
// ISO8601 format
console.log( moment().format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Related
I am trying to format a string date to yyyy-mm-dd. Typically, it comes as d/m/yyyy but regardless of the input, I want to have it as yyyy-mm-dd.
const date = new Date("13/10/2016");
const DateParsed = moment(date).format("YYYY-MM-DD");
const dateParsed2 = moment("13/10/2016").format('YYYY-MM-DD')
I was expecting 2016-10-13 but I'm getting 0000-00-00 instead.
Since your input is not in a format recognized by Date.parse, I suggest to use moment(String, String) instead of new Date().
Here a live sample:
const date = "13/10/2016";
const DateParsed = moment(date, 'DD/MM/YYYY').format("YYYY-MM-DD");
console.log(DateParsed);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
How can I use momentjs to format a number in seconds like
moment(172800).format('hh[h] mm[min]')
to some like
'48h 00min'
in essential I want to count timer pass in hh:mm:ss with hours going way beyond 24.
You can use moment-duration-format plug-in.
You can create a duration from your seconds and then use format method from moment-duration-format to print duration according your needs.
Here a example:
// Create moment duration
var dur = moment.duration(172800, 's');
// Format duration according your needs
console.log( dur.format('hh[h] mm[min]') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
I want to convert the time from the standard 12-hour reading to the 24-hour one in simpleWeather. When I try to do it, it returns the time in the Unix epoch format.
$(function sunset(){
var sunset=
$.simpleWeather({
location:'Dalol,Afar',
woeid:'',
units:'f',
success:function(weather){
var Sunset= moment(weather.sunset, "HH:mm:ss");
html='Sunset: '+Sunset;
$('#sunset').html(html);
}
});
});
I had contacted the developer on how to do convert it using moment.js, but he didn't say much more than that. How can the code be corrected to display the desired format?
You need to first parse and then format the weather.sunset with the right formatting:
var sunset = moment(weather.sunset, ["h:mm A"]).format("HH:mm");
simpleWeather.js returns the sunset as "5:31 pm" so the corresponding moment format to parse this date is h:mm A, where the A captures the pm/am. To format it in the 24-hour notation, format("HH:mm") will do the job.
I want to store the current GMT date and time in ISODate format in mongodb.
Similarly to this
ISODate("2012-07-26T17:43:25.678Z")
I want to store the result in mongodb in the above mentioned format(it should be a ISODate object). How to generate such a format in using php?
I think DateTime will fit into an ISODate, but you might also want to consider http://php.net/manual/en/datetime.setisodate.php.
Use MongoDate: http://www.php.net/manual/en/class.mongodate.php
Example in this page:
<?php
// save a date to the database
$collection->save(array("ts" => new MongoDate()));
$start = new MongoDate(strtotime("2010-01-15 00:00:00"));
$end = new MongoDate(strtotime("2010-01-30 00:00:00"));
// find dates between 1/15/2010 and 1/30/2010
$collection->find(array("ts" => array('$gt' => $start, '$lte' => $end)));
?>
How to validate particular format date string using Javascript?
I have one date picker which has the display format like "dddd MMMM dd, yyyy"(displaying like this:"Wednesday February 03, 2010".) So i have to validate this format using javascript. Please help me for implementing this..
If you want to check exactly that format, you could use regular expression:
var re = new RegExp( '^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)\\s*(January|February|March|April|May|June|July|August|September|November|December)\\s*(\\d\\d),\\s*(\\d{2,4})$' );
var date = 'Wednesday February 03, 2010';
if ( ( match = date.match( re ) ) != null )
{ // valid
alert( match );
}
Or if you just need to know if it is a valid date, what format ever, simply convert it:
var dateSec, dateObj, dateStr = 'Wednesday February 03, 2010';
dateSec = Date.parse( dateStr ); // unix timestamp
if ( dateSec ) // not NaN
dateObj = new Date( dateSec ); // date object
If your application is going to require date manipulation methods, you may want to consider using something like the Datejs library.
If you opt for Datejs, you can use the parseExact() method for the validation. It will return a date object if the date is valid, or null if the date is invalid.
Native JavaScript support for date formatting and validation is somewhat limited.
Take a look at http://www.datejs.com/
You can do stuff like Date.parse('my date string')
Datejs or Dojo can do this. With dojo.date.locale.parse:
var dateAsString = "Wednesday February 03, 2010";
var dateObject = dojo.date.locale.parse(dateAsString, {datePattern: "EEEE MMMM dd, yyyy", selector: "date", locale: "en"});
dateObject will contain the Date object, or null if the string does not match the specified pattern. This can work with a fixed language or any native language.
It doesn't seem right that a date picker would use this as a serialized Date format, though. It should use something easier to parse, like ISO8601 representation.