I have a datetime string in the following format.
var datetime="Thu May 5 05:30:00 UTC+0530 2011" ;
I want to convert it in the following format. How can I do it in javascript
"Thursday, 05 May 2011"
The globalize plugin has date parsing and formatting features.
Here is an example from the plugin page:
Globalize.format( new Date(1955,10,5), "dddd MMMM d, yyyy" ); // "Saturday November 5, 1955"
The date.js library is very useful for working with Dates.
Formatting examples:
Date.today().toString("dddd MMMM d, yyyy"); // Monday November 19, 2007
Date.today().toString(); // native .toString() functionality
Date.today().toString("M/d/yyyy"); // 11/19/2007
Date.today().toString("d-MMM-yyyy"); // 19-Nov-2007
new Date().toString("HH:mm"); // 18:45
Parsing examples:
Date.parse('today');
Date.parse('t + 5 d'); // today + 5 days
Date.parse('next thursday');
Date.parse('February 20th 1973');
Date.parse('Thu, 1 July 2004 22:30:00');
Related
Here is my date string and i want to convert this into javascript date object.
Input: -- Wednesday, March 4th, 2020, 5:00:00 pm
Expected result: -- 2020-03-13T15:04:16.913Z'
I tried :
moment('Wednesday, March 4th, 2020, 5:00:00 pm').format('YYYY-MM-DDTHH:mm:ss\\Z')
You need to inform Moment about the format of the string you're giving it.
From the documentation / display :
Wednesday = dddd
March = MMMM
4th = Qo
2020 = YYYY
5:00:00 pm = h:mm:ss a
So yout input format is "dddd, MMMM Qo, YYYY, h:mm:ss a"
Now you can create a valid Moment and manipulate it.
moment('Wednesday, March 4th, 2020, 5:00:00 pm', "dddd, MMMM Qo, YYYY, h:mm:ss a").format(whatever)
Firstly you need to inform moment the format of the expected input , followed by format() to convert the same into required format.
Based on their docs: https://momentjs.com/docs/#/displaying/
Check this code out. It will help you get started:
var input = "Wednesday, March 4th, 2020, 5:00:00 pm";
var date = moment(input, "dddd, MMMM Do, YYYY, h:mm:SSSS a").format(
"YYYY-MM-DDTHH:mm:ss\\Z"
);
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
I want to convert the current date and time to in the following way using moment.js.
Current date and time using javascript new Date(): Thu Jul 12 2018 09:28:51 GMT+0530 (India Standard Time) and want to convert the above format to below mentioned format.
1. Thu, 12 Jul 2018 09:31:37 GMT
2. 2018-07-12T09:31:38Z
You can learn more about formatting with moment.js here.
Escape words in formatting with escaping-characters "[]".
console.log(moment());
console.log(moment().format('ddd, DD MMM YYYY HH:mm:ss [GMT]'));
console.log(moment().format('YYYY-MM-DD[T]HH:mm:ss[Z]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
you can also try like below for
nodejs
var moment = require('moment');
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
angular
import moment from 'moment';
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
Install moment like below
npm install --save moment
Html javascript
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
moment source script
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can use toGMTSting method to convert local time into GMT format. Hope this helps..
console.log(new Date().toGMTString());
console.log(new Date("Fri Jan 20 2012 11:51:36 GMT-0530").toGMTString());
You can try these also:
var moment = require('moment');
let startDate= new moment('11/22/1990','MM/DD/YYYY').format("YYYY/MM/DD");
You can also understand these:
let otherDate= 1399919400000;
var neweDate = moment(otherDate).format('DD/MM/YYYY');
//My neweDate output is "13/05/2014";
moment.locale('cs');
console.log(moment.locale()); // en
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
You can verify the date also:
moment("not a real date").isValid(); // false
moment("2010 13", "YYYY MM").isValid(); // false (not a real month)
moment("2010 11 31", "YYYY MM DD").isValid(); // false (not a real day)
moment("2010 2 29", "YYYY MM DD").isValid(); // false (not a leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)
You can create a Moment with a pre-existing native Javascript Date object.
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
You can create a moment with an array of numbers that mirror the parameters passed to new Date()
[year, month, day, hour, minute, second, millisecond]
moment([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM
Any value past the year is optional, and will default to the lowest possible number.
moment([2010]); // January 1st
moment([2010, 6]); // July 1st
moment([2010, 6, 10]); // July 10th
I want to use moment (or any other method) to validate a particular string as a valid date value based on the default javascript date format.
I have the following code:
var date = new Date(); // Tue Nov 28 2017 17:54:41 GMT-0600 (CST)
I want to take that string output and use moment to validate that as a valid date string. I know I can do this:
moment(date, moment.ISO_8601).isValid();
But there is a problem. Here are the results for the same date values:
moment(new Date(), moment.ISO_8601).isValid(); //True
moment("Tue Nov 28 2017 17:54:41 GMT-0600 (CST)", moment.ISO_8601).isValid() //False
Which is not what I want since both of them should be true. I can't do new Date(...) because here's the results for that approach:
moment(new Date("Tue Nov 28 2017 17:54:41 GMT-0600 (CST)"), moment.ISO_8601).isValid(); //True
moment(new Date("3"), moment.ISO_8601).isValid(); //True
This also is not what I want since the second line should be false.
How do I properly valid a string as a valid date only if it strictly follows the format Tue Nov 28 2017 17:54:41 GMT-0600 (CST)?
I have researched everywhere to understand what format Tue Nov 28 2017 17:54:41 GMT-0600 (CST) is but I can't find anything. It's not ISO 8601 and not RFC 2822 either.
As RobG stated in the comments There is no "default javascript date format". The output from Date.prototype.toString is implementation dependent
Since your input is neither in ISO 8601 nor in RFC 2822 Date time formats you have to use moment(String, String) using 'ddd MMM D YYYY HH:mm:ss [GMT]Z' as format parameter. If you want that your string matches strictly your format, you can use strict mode (moment(String, String, Boolean)).
Please note that moment does not provide token for parsing timezone abbreviation (e.g. CST), so your input will not be recognized in strict mode. Here an example of parsing using both strict and forgiving mode; myIsValid function is an example of removing timezone abbreviation from the input.
var s = 'Tue Nov 28 2017 17:54:41 GMT-0600 (CST)';
var fmt = 'ddd MMM D YYYY HH:mm:ss [GMT]Z';
console.log(moment(s, fmt).isValid());
console.log(moment(s, fmt, true).isValid());
function myIsValid(input){
// Check if input contains ()
var idxl = input.indexOf('(');
var idxr = input.indexOf(')');
if( idxl>0 && idxr>0 ){
// Remove timezone abbreviation
input = input.substr(0, idxl-1);
}
var fmt = 'ddd MMM D YYYY HH:mm:ss [GMT]Z';
return moment(input, fmt, true).isValid();
}
console.log(myIsValid(s));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Lastest version of moment (2.19.2), gives Invalid Date even with forgiving mode. Using parsingFlags() you see that weekdayMismatch is true, I think this is an issue of the 2.19+ version of moment this is related to moment #4227 issue.
var s = 'Tue Nov 28 2017 17:54:41 GMT-0600 (CST)';
var fmt = 'ddd MMM D YYYY HH:mm:ss [GMT]Z';
var m1 = moment(s, fmt);
var m2 = moment(s, fmt, true);
console.log(m1.isValid());
console.log(m1.parsingFlags());
console.log(m2.isValid());
console.log(m2.parsingFlags());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
If you want to strictly validate a timestamp with Moment.js, use the strict flag when parsing:
var s = 'Tue Nov 28 2017 17:54:41 GMT-0600';
s = s.replace('GMT','');
console.log('Modified string: ' + s);
var mStrict = moment(s, 'ddd MMM D YYYY HH:mm:ss ZZ', true);
console.log('Strict parse: ' + mStrict.format());
var mLoose = moment(s, 'ddd MMM D YYYY HH:mm:ss ZZ');
console.log('Loose parse : ' + mLoose.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
However, for me both strict and loose produce "Invalid date", which is incorrect (and should be "Invalid Date"). The issue seems to be the timezone value or token.
This question already has answers here:
How to convert Moment.js date to users local timezone?
(5 answers)
Closed 6 years ago.
I am using moment.js and moment-timezone. how can I create a function to convert 2016-06-12 06:22:18 UTC time to the user's timezone time and then format the time to Jun 12, 2015 6:22 PM
for example:
updateTime("2016-06-12 06:22:18");
if in chicago, the output will be:
"Jun 12, 2016, 1:22 AM"
You can try something like this.
moment.utc("2016-06-12 06:22:18", "YYYY-MM-DD HH:mm:ss").tz('America/Chicago').format("MMM D, YYYY, hh:mm a")
Hey this links may help you
https://gist.github.com/founddrama/2182389
https://www.sitepoint.com/managing-dates-times-using-moment-js/
http://momentjs.com/timezone/docs/
Examples:
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 20th 2016, 11:10:20 am
moment().format('dddd'); // Monday
moment().format("MMM Do YY"); // Jun 20th 16
moment().format('YYYY [escaped] YYYY'); // 2016 escaped 2016
moment().format();
moment.tz('2016-01-01', 'America/Chicago').format('z');
You might want to look at this answer, perhaps: Get current timestamp from specific timezone
They solved by taking the time in UTC and converting it in the specific time
var utcEpochSeconds = dateObj.getTime() + (dateObj.getTimezoneOffset() * 60000);
The function you need would be like this :
function getLocalDate (input)
{
var dt= moment.utc(input);
dt.add(moment().utcOffset(),'minutes');
return moment(dt).format('MMM D, YYYY, hh:mm a');
};
Or this :
function getLocalDate(input)
{
return moment.utc(input).local().format('MMM D, YYYY, hh:mm a');
};
How to parse a string into a date object at JavaScript (without using any 3d party) that is at dd-MM-yyyy HH:mm (all of them numbers) format?
var p = "04-22-1980 12:22".split(/-|\s+|:/);
// new Date(year, month, day [, hour, minute, second, millisecond ])
new Date(p[2], p[0] - 1, p[1], p[3], p[4]);
// => Tue Apr 22 1980 12:22:00 GMT-0500 (Central Daylight Time)
DateJS is your friend: http://www.datejs.com/
It parses pretty much anything reasonable you throw at it:
// Convert text into Date
Date.parse('today');
Date.parse('t + 5 d'); // today + 5 days
Date.parse('next thursday');
Date.parse('February 20th 1973');
Date.parse('Thu, 1 July 2004 22:30:00');
It's not perfect, but it does a pretty good job.