Convert date and time string to valid date - momentjs

I have the following string.
"2021-07-01 10:00"
How can I convert this into a valid date with moment.js?
I tried the below, it says invalid date.
moment("2021-07-01 10:00", "YYYY-MM-DD HH:mm")

Try some something like this:
var res = moment("2021-07-01 10:00", "YYYY-MM-DD hh:mm").format(
"YYYY-MM-DD hh:mm"
);
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

I realised, nothing is wrong with this script. I used a variable instead of the string and I realised that was an object: {_text: "2021-07-01 10:00"}

Related

MomentJS can't convert a date with format YYYY-MM-DD HH:mm:ss.fff Z

I am trying to convert the next date using MomentJS:
const moment = require('moment');
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime);
But it fails and the next message appears:
Deprecation warning: value provided is not in a recognized RFC2822 or
ISO format. moment construction falls back to js Date(), which is not
reliable across all browsers and versions. Non RFC2822/ISO date formats
are discouraged and will be removed in an upcoming major release.
Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more
info.
Snippet showing the issue:
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
I also tried using:
moment.utc(datetime);
but failed.
As moment(String) docs says:
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
2017-11-19 02:45:22.011 +00:00 is not in ISO 8601 compliant format because there is a space between fractional seconds and UTC offset (2017-11-19 02:45:22.011+00:00 is an ISO 8601 version of your input). So you can use moment(String, String), here a live sample:
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime, 'YYYY-MM-DDTHH:mm:ss.fff Z');
console.log(newDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
Reformatting the date string from:
2017-11-19 02:45:22.011 +00:00
To:
2017-11-19T02:45:22.011Z
Solved it

How to convert from UTC to local time in moment.js?

I have a simple use case but I cannot figure out how to convert from GMT/UTC to local time in moment.js.
Example:
var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH").format('YYYY-MMM-DD h:mm A');
console.log(gmtDateTime) emits 2015-Oct-24 8:00 PM, which is correct. Now I simply want to convert this to my local time, which happens to be Mountain Daylight Time. So the correct converted date would be 2015-Oct-24 2:00 PM, because I am 6 hours earlier than GMT/UTC.
How can this be done simply with moment.js?
Try moment().local().
Example:
var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH")
var local = gmtDateTime.local().format('YYYY-MMM-DD h:mm A');
However, #antti-kuosmanen answer is accepted and correct.
moment().local() is a correct way. But this can do in a single line. Here is how
var local = moment.utc("2015-10-24 20:00").local().format('YYYY-MMM-DD h:mm A');
No need to formating twice for the results
Cheers!! Read Simple Write Simple

Convert datepicker date to ISO format with moment.js

My task is to use a datepicker to pick a date in the prescribed format, eg(MM-DD-YYYY) and pass it to the server as ISO formatted.
While it test the output the ISO formatted date is one day behind.
For example
If i select
07-13-2015
My Output ISO format is
ISO format is :2015-07-12T18:30:00.000Z
Here you can see date is 13 but the output date is 12
I am from India. I tried with zone and utcOffset, ended up with no results. How do i set it right
Here is the JSFIDDLE
js code
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(moment(selectedDate).toISOString());
});
I do have a hidden field which value will be updated on change and that will be processed in the server. No issues on that.
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(selectedDate.toISOString());
});
Your selectedDate is already a moment object so you do not need to feed it back into another moment.
Example:
var test = '07-13-2015'
var mtest = moment(test,"MM-DD-YYYY")
mtest.toISOString()
"2015-07-13T06:00:00.000Z"
Your could try converting the date format to UTC at once.
selectedDate = moment(selectedDate).utc('MM-DD-YYYY')
According to http://dygraphs.com/date-formats.html, if you pass a string like '07-13-2015', it means Midnight of 13th July 2015. Now, if you use toISOString function, it will convert it to UTC by default. To not convert it to UTC, just pass a parameter true in the toISOString function. (Moment.js docs)
For example:
var date = '07-13-2015';
date = moment(date,'MM-DD-YYY');
console.log(date.toISOString(true));
This way, moment will not convert the date to UTC.

How to convert DateTime object to string in Crystal Reports

I have a simple line of Crystal Reports code below:
EffectiveDateTimeString = ToText({Command.EffectiveDate} , "dd-MM-yyyy hh:mm:ss" );
However, when I try to validate the formula, I get a error saying "Too many arguments have been given to this function" with "dd-MM-yyyy hh:mm:ss" selected. Command.EffectiveDate is the DateTime object. How can I convert this to a string?
Thanks!
You need to use the assignment operator :=, not the equivalency one =:
EffectiveDateTimeString := ToText({Command.EffectiveDate} , "dd-MM-yyyy hh:mm:ss" );
*edit *
This snippet works as expected:
ToText(CurrentDate + CurrentTime, "dd-MM-yyyy hh:mm:ss");
Ensure that your field is actually returning a date/time, rather than one or the other.
Try this:
EffectiveDateTimeString := CStr(DateTime({Command.EffectiveDate} , "dd/MM/yyyy hh:mm:ss" ));
If {Command.EffectiveDate} is not in the right format, this will ensure that it is indeed DateTime.
If that doesn't work, I just created a simple formula in a sample report of mine, and the below code worked just fine on a DateTime field:
stringVar EffectiveDateTimeString;
EffectiveDateTimeString := CStr({Command.EffectiveDate}, "dd/MM/yyyy hh:mm:ss");
EffectiveDateTimeString

Check String if the format is "yyyy-MM-dd h:mm:ss" in VB.NET

How to check if the given string is in a valid customized format "yyyy-MM-dd h:mm:ss"?
For example:
2013-09-09 05:25:40
is in a valid format.
and
09-09-2013 05:25:40
is invalid format.
You can use DateTime.TryParseExact() for this.
if(DateTime.TryParseExact(yourDate, "yyyy-MM-dd h:mm:ss", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dateValue))
{
// DateTime parsed, dateValue contains the parsed DateTime
}

Resources