How can i change the current date to this format(DD/MM/YYYY) using moment.js?
I have tried below code.
$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");
But it's return 0037-11-24T18:30:00.000Z. Did't help to format current date.
You need to call format() function to get the formatted value
$scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
//or $scope.SearchDate = moment().format("DD/MM/YYYY")
The syntax you have used is used to parse a given string to date object by using the specified formate
You can use this
moment().format("DD/MM/YYYY");
However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.
var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
This worked for me
var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP
moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
This actually worked for me:
moment(mydate).format('L');
for anyone who's using react-moment:
simply use format prop to your needed format:
const now = new Date()
<Moment format="DD/MM/YYYY">{now}</Moment>
A safe way to do this
moment.locale('en-US');
moment().format("L");
"06/23/2021"
moment.locale('fr');
moment().format("L");
"23/06/2021"
Related
I create a date with Luxon in the following way:
const date = DateTime.utc(2000, 6, 23).toFormat('yyyy-MM-dd')
Then I try to convert it to a JS date by doing this:
const jsDate = new Date(date)
Finally, I convert it back to the 'yyyy-MM-dd' format with
const parsedDate = DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd')
But instead of giving me 2000-06-23 it gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
I try to convert it to a JS date by doing const jsDate = new Date(date)
Uh, why not just date.toJSDate()?
Instead of giving me 2000-06-23, DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd') gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
According to its docs, fromJSDate defaults to creating a DateTime instance in the local (system) timezone. If you expect another UTC date back, you need to specify that:
DateTime.fromJSDate(jsDate, {zone: 'utc'}).toFormat('yyyy-MM-dd')
I've a moment object to which I'm trying add some days.
However, it is returning the same moment object as a result.
But, if I simply try to do it on the current date, it works fine.
Also, note that, I always receive a moment object to which I need to add the days.
Code:
const someDate = moment('22-03-2020');
console.log(someDate.add(5, 'days');
someDate is something I receive from the server and is always a moment object.
How do I fix this?
You should specify what format you used, like so:
const someDate = moment('22-03-2020', 'DD-MM-YYYY')
const newDate = someDate.add(5, 'days')
console.log(newDate)
To format a moment object, you just have to add .format() like so:
console.log(newDate.format())
"Deprecation warning in moment js - Not in a recognized ISO format"
if you provide not valid ISO format for more check here information
Deprecation warning in moment js - Not in a recognized ISO format
// Recommended format: YYYY/MM/DD
const someDate = moment('2020-03-22');
console.log(someDate.add(5, 'days').format("YYYY-MM-DD"));
Test here
I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.
any help would be appreciated.
This code may solve your problem.
moment("07/2017", "MM/YYYY").subtract(1, 'months').format('YYMM');
DEMO at https://jsfiddle.net/nffswx75/
var dt = "07/2017";
alert(moment(dt,"MM/YYYYY").format('YYMM'));
alert(moment(dt,"MM/YYYYY").add(-1, 'months').format('YYMM'));
alert(moment(dt,"MM/YYYYY").subtract(1, 'months').format('YYMM'));
You can let moment create a date object from a string by telling it what format your date is in.
let dateString: string = "07/2017";
var date = moment(dateString, "MM/YYYY").subtract(1, 'month').format("YYMM");
I am trying to get the date time in moment js in this format :
2016-12-19T09:43:45.672Z
The problem is I am able to get the time format as
2016-12-19T15:04:09+05:30
using
moment().format();
but I need the format as the first one [like .672Z instead of +05:30]. Any suggestions would be of great help.
From the documentation on the format method:
To escape characters in format strings, you can wrap the characters in square brackets.
Since "Z" is a format token for the timezone, you need to escape it. So the format string you are after is:
moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
As #VincenzoC said in a comment, the toISOString() method would also give you the format you are after.
Use moment.utc() to display time in UTC time instead of local time:
var dateValue = moment().utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
or moment().toISOString() to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ, the timezone is always UTC):
var dateValue = moment().toISOString();
Try this
const start_date = '2018-09-30';
const t = moment(start_date).utc().format();
console.log(t);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
var dateTime = new Date("2015-06-17 14:24:36");
dateTime = moment(dateTime).format("YYYY-MM-DD HH:mm:ss");
Try this. You should have the date in the correct format.
//In Node/JS
myDate = moment(data.myTime.format('YYYY/MM/DD HH:MM:SS')).toISOString();
//myDate shows '2014-09-24T04:09:00.000Z'
Insert INTO (dateColumn..) Values(myDate)...
This is the error I get after inserting, note column in Mysql is a "datetime" type.
MySQL Error:: { [Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: '2014-09- 24T04:09:00.000Z' for column '_dateColumn' at row 1]
code: 'ER_TRUNCATED_WRONG_VALUE',
This result happens because you are using the toISOString() method and it is not a valid format to insert into your DATETIME column. The correct format probably is YYYY-MM-DD HH:MM:SS(I think it depends on MySQL configuration, but this is the default) as the docs points out.
So you should try using moment's format() method like this:
myDate = moment(data.myTime.format('YYYY/MM/DD HH:mm:ss')).format("YYYY-MM-DD HH:mm:ss");
In fact, I don't know what data.myTime is, but if its a moment object too, you can change the first format() method and remove the second one.
DontVoteMeDown answer is right, except that the minutes 'mm' and seconds 'ss' need to be in lowercase, otherwise a wrong value is returned:
myDate = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
Also, you should check the value sent by javascript before to do your SQL query, in PHP:
DateTime::createFromFormat('Y-m-d H:i:s', $myDate);
Will return false if the date is misformatted.
Here's a function extension to format it into the MySQL DateTime format
moment.prototype.toMySqlDateTime = function () {
return this.format('YYYY-MM-DD HH:mm:ss');
};
You'll be able to do: moment().toMySqlDateTime();
(I'm working with Node.js, Express and MySql and this has worked for me:)
var momentDate=new moment('2018-08-31T20:13:00.000Z');
var readyToInsert=momentDate.format("YYYY-MM-DD HH:mm:ss");
To generate mysql format datetime you can use this:
var moment = require('moment');
let currentTimestamp = moment().unix();//in seconds
let currentDatetime = moment(currentTimestamp*1000).format("YYYY-MM-DD HH:mm:ss");//mysql datetime.
currentTimestamp = moment(currentDatetime).valueOf();//current timestamp in milliseconds
moment().format("YYYY-MM-DD HH:mm:ss")
I think this is the correct format. Otherwise you get the months inserted in your minutes, and you also get seconds above 60.
myDate = moment(data.myTime.format('YYYY/MM/DD HH:MM:ss')).format("YYYY-MM-DD HH:MM:SS");
Tested in Chrome with Moment.js 2.9.0