converting date formate with moment - momentjs

This Meteor server code needs to get "06 2008" from "Fri Jun 06 00:00:00 BST 2008" but I am getting "Invalid date", Any ideas? Thanks
let dat = new Date('Fri Jun 06 00:00:00 BST 2008');
let mDat = moment(dat).format("dd yyyy");
console.log(mDat);

it Doesn't recognize your Time Standard.. so remove and try .. if you want date use DD for year use YYYY its case sensitive
let dat = new Date('Fri Jun 06 00:00:00 2008');
let mDat = moment(dat).format("DD YYYY");
console.log(mDat);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Here's the simple solution-
(JS BIN link - https://jsbin.com/nihoxivoxa/edit?js,console)
var date_text = 'Fri Jun 06 00:00:00 BST 2008';
var date_text = date_text.split('BST').join();
var dat = new Date(date_text);
var mDat = moment(dat).format("DD YYYY");
console.log(mDat);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/scriptaculous/1/scriptaculous.js"></script>
</body>
</html>

Related

Formatting bootstrap-datepicker result

So here is how i setup my datepicker
$('#picker').datepicker({
format: 'yyyy-mm-dd',
multidate: true
});
and then here is how i showing the result
$('#picker').on('changeDate', function(event) {
console.log( $('#picker').datepicker("getDates"));
});
here is the result from console
[Tue Jun 02 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat), Wed Jun 10 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat), Wed Jun 17 2020 00:00:00 GMT+0700 (Waktu Indonesia Barat)]
How can i make it to yyyy-mm-dd format ?
So here is how i fix it
$('#picker').datepicker("getFormattedDate")
maybe it will help others
Give moment.js a try. It is a very powerful tool not just only to format Date objects, but to manipulate them and make custom validations.

Moment JS getting Date without time [duplicate]

formatCalendarDate = function (dateTime) {
return moment.utc(dateTime).format('LLL');
};
It displays: "28 februari 2013 09:24"
But I would like to remove the time at the end. How can I do that?
I'm using Moment.js.
Sorry to jump in so late, but if you want to remove the time portion of a moment() rather than formatting it, then the code is:
.startOf('day')
Ref: http://momentjs.com/docs/#/manipulating/start-of/
Use format('LL')
Depending on what you're trying to do with it, format('LL') could do the trick. It produces something like this:
Moment().format('LL'); // => April 29, 2016
The correct way would be to specify the input as per your requirement which will give you more flexibility.
The present definition includes the following
LTS : 'h:mm:ss A',
LT : 'h:mm A',
L : 'MM/DD/YYYY',
LL : 'MMMM D, YYYY',
LLL : 'MMMM D, YYYY h:mm A',
LLLL : 'dddd, MMMM D, YYYY h:mm A'
You can use any of these or change the input passed into moment().format().
For example, for your case you can pass moment.utc(dateTime).format('MMMM D, YYYY').
Okay, so I know I'm way late to the party. Like 6 years late but this was something I needed to figure out and have it formatted YYYY-MM-DD.
moment().format(moment.HTML5_FMT.DATE); // 2019-11-08
You can also pass in a parameter like, 2019-11-08T17:44:56.144.
moment("2019-11-08T17:44:56.144").format(moment.HTML5_FMT.DATE); // 2019-11-08
https://momentjs.com/docs/#/parsing/special-formats/
You can also use this format:
moment().format('ddd, ll'); // Wed, Jan 4, 2017
formatCalendarDate = function (dateTime) {
return moment.utc(dateTime).format('LL')
}
Look at these Examples.
Format Dates
moment().format('MMMM Do YYYY, h:mm:ss a'); // December 7th 2020, 9:58:18 am
moment().format('dddd'); // Monday
moment().format("MMM Do YY"); // Dec 7th 20
moment().format('YYYY [escaped] YYYY'); // 2020 escaped 2020
moment().format(); // 2020-12-07T09:58:18+05:30
Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 8 years ago
moment().startOf('day').fromNow(); // 10 hours ago
moment().endOf('day').fromNow(); // in 14 hours
moment().startOf('hour').fromNow(); // an hour ago
Calendar Time
moment().subtract(10, 'days').calendar(); // 11/27/2020
moment().subtract(6, 'days').calendar(); // Last Tuesday at 9:58 AM
moment().subtract(3, 'days').calendar(); // Last Friday at 9:58 AM
moment().subtract(1, 'days').calendar(); // Yesterday at 9:58 AM
moment().calendar(); // Today at 9:58 AM
moment().add(1, 'days').calendar(); // Tomorrow at 9:58 AM
moment().add(3, 'days').calendar(); // Thursday at 9:58 AM
moment().add(10, 'days').calendar(); // 12/17/2020
Multiple Locale Support
moment.locale(); // en
moment().format('LT'); // 9:58 AM
moment().format('LTS'); // 9:58:18 AM
moment().format('L'); // 12/07/2020
moment().format('l'); // 12/7/2020
moment().format('LL'); // December 7, 2020
moment().format('ll'); // Dec 7, 2020
moment().format('LLL'); // December 7, 2020 9:58 AM
moment().format('lll'); // Dec 7, 2020 9:58 AM
moment().format('LLLL'); // Monday, December 7, 2020 9:58 AM
moment().format('llll'); // Mon, Dec 7, 2020 9:58 AM
Whenever I use the moment.js library I specify the desired format this way:
moment(<your Date goes here>).format("DD-MMM-YYYY")
or
moment(<your Date goes here>).format("DD/MMM/YYYY")
... etc I hope you get the idea
Inside the format function, you put the desired format. The example above will get rid of all unwanted elements from the date such as minutes and seconds
With newer versions of moment.js you can also do this:
var dateTime = moment();
var dateValue = moment({
year: dateTime.year(),
month: dateTime.month(),
day: dateTime.date()
});
See: http://momentjs.com/docs/#/parsing/object/.
You can use this constructor
moment({h:0, m:0, s:0, ms:0})
http://momentjs.com/docs/#/parsing/object/
console.log( moment().format('YYYY-MM-DD HH:mm:ss') )
console.log( moment({h:0, m:0, s:0, ms:0}).format('YYYY-MM-DD HH:mm:ss') )
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
For people like me want the long date format (LLLL) but without the time of day, there's a GitHub issue for that: https://github.com/moment/moment/issues/2505. For now, there's a workaround:
var localeData = moment.localeData( moment.locale() ),
llll = localeData.longDateFormat( 'llll' ),
lll = localeData.longDateFormat( 'lll' ),
ll = localeData.longDateFormat( 'll' ),
longDateFormat = llll.replace( lll.replace( ll, '' ), '' );
var formattedDate = myMoment.format(longDateFormat);
Try this:
moment.format().split("T")[0]
The thing is - you can run into an issue with timezones. For example, if you parse date like this: '2022-02-26T00:36:21+01:00' it may turn into '25/02/2022' As a solution if your date is in ISO format you can just cut off the time portion from the string, like this:
moment('2022-02-26T00:36:21+01:00'.split('T')[0]).utc().format('DD/MM/YYYY')
This solution is quite blunt, so be careful with string format.
This format works pretty fine
const date = new Date();
const myFormat= 'YYYY-MM-DD';
const myDate = moment(date, 'YYYYMMDDTHHmmss').format(myFormat);
Try
new Date().toDateString()
Result - "Fri Jun 17 2022"
This worked perfectly for me:
moment().format('YYYY-MM-DD')
moment(date).format(DateFormat)
Here DateFormat should be DateFormat = 'YYYY-MM-DD'

How to convert the month name in english text in datetime to arabic text using C#?

I have a date which is displayed on 12 July 2013. I am using the format "dd MMMM yyyy". I want to display the month text i.e. July in Arabic text. Is there anyway?
Am getting يوليه as result instead يوليو, My client, saying: the month of July is “يوليو” in Arabic, while on the website it is showing as “يوليه”. Which is wrong,
can anyone help me on this?
Please find the code below.
What I have tried:
CultureInfo uiCulture1 = CultureInfo.CurrentUICulture; //'ar-AE'
DateTime dDateTime2 = DateTime.Parse(dt.ToString(), uiCulture1, System.Globalization.DateTimeStyles.AssumeLocal);
lblDate.Value = dt.ToString("dd MMMM yyyy");
try this
var dateTime = DateTime.Now;
var month = DateTime.Now.ToString("MMMM", new CultureInfo("ar-AE"));
Console.WriteLine(month);
result أكتوبر
I think the issue is the
dt.ToString()
in the Date.Parse. The dDateTime2 is getting set to 07 December instead of 12 July because I think the Culture in question is defaulting to dd-mm-yyyy for date format.
var dt = new DateTime(2013, 07, 12);
Console.WriteLine(dt.ToString("dd MMMM yyyy")); //12 July 2013
CultureInfo uiCulture1 = new CultureInfo("ar-AE"); //'ar-AE'
DateTime dDateTime2 = DateTime.Parse(dt.ToString(), uiCulture1, System.Globalization.DateTimeStyles.AssumeLocal);
Console.WriteLine(dDateTime2.ToString("dd MMMM yyyy")); // 07 December 2013
Console.WriteLine(dt.ToString("dd MMMM yyyy", uiCulture1)); //12 يوليو 2013
provide the culture info in dateTime.toString()
var dateTime = DateTime.Now;
var uiCulture = new CultureInfo("ar-AE");
Console.WriteLine(dateTime.ToString("dd MMM yyyy",uiCulture));

Moment.js wierd translation

I'm using moment.js and getting something strange:
Input string:
'Wed, 30 Aug 2017 19:53:54 EST'
Want parse it using Moment.js:
moment('Wed, 30 Aug 2017 19:53:54 EST', 'ddd, DD MMM YYYY HH:mm:ss z');
getting object:
_d: Wed May 31 2017 23:59:59 GMT+0300 (FLE Daylight Time) {}
_f: "ddd, DD MMM YYYY HH:mm:ss z"
_i: "Wed, 30 Aug 2017 19:53:54 EST"
_isAMomentObject: true
_isUTC: false
_isValid: true
_i - it's input
_f - as I can understand - format
_d - it's date, result of parsing, WHY there 'May 31'?
Found and fixed problem. If someone faced with same problem, they could check my solution.
Datetime object mutate in controller:
let myDate = moment();
------------ skip ------------
let someObj = {
yesterday: myDate.subtract(1, 'days')
};
Because it's object, definitions like below doesn't helps:
const myDate = moment();
or
let myDate = moment();
Object.freeze(myDate);
only work solution for me is:
let myDate = moment();
------------ skip ------------
let someObj = {
yesterday: myDate.clone().subtract(1, 'days')
};

Copying cookie for httr

I'm trying to access a site that sets a cookie on the basis of what type of visitor:
library(httr)
url <- "https://www.blackrock.com/ca/individual/en/products/product-list#categoryId=1&lvl2=overview"
Essentially everything I've tried returns the this...:
GET(url)
Response [https://www.blackrock.com/ca/individual/en/site-entry?targetUrl=%2Fca%2Findividual%2Fen%2Fproducts%2Fproduct-list%3FsiteEntryPassthrough%3Dtrue]
Status: 200
Content-type: text/html;charset=UTF-8
Size: 270 kB
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#" lang="en" xml:lang="en"><head><meta content="text/html;charset=UTF-8" http-equiv="Content-type...
<meta name="description" content="" />
<meta name="robots" content="noindex,noarchive,nocache" />
<meta property="og:title" content="Site Entry" />
<meta property="og:type" content="website" />
<meta property="og:image" content="/amer-retail-assets/include/common/images/blackrock_logo.png" />
<meta property="og:site_name" content="BlackRock" />
<meta property="og:locale" content="en_CA" />
<meta property="og:url" content="https://www.blackrock.com/ca/individual/en/site-entry?locale=en_CA" />
<link rel="canonical" href="https://www.blackrock.com/ca/individual/en/site-entry?locale=en_CA" />
So, instead I tried copying the cookie after manually passing the gateway or site-entry page in chrome:
cookie = c(`JSESSION_amer-retail01` = "AC91C17F269D8F6A136E576531FA6E05",
`UnicaID` = "10.39.18.249-1408392737241856",
`UnicaNIODID` = "NQHKNKQYKDm-YxUEYgX",
`_ga` = "GA1.2.127078965.1408392737",
`blkUserType-ca-one` = "institutional",
`s_pers` = "%20s_fid%3D14569E53C0F4EE51-3982590542B2DDA4%7C1471566685393%3B%20gpv%3Dca-one%257Cproducts%257Cproduct%2520list%7C1408410085400%3B%20s_nr%3D1408408285405-Repeat%7C1439944285405%3B%20s_pers_eVar15%3Dprospect%7C1411000285408%3B%20s_pers_prop19%3Danonymous%7C1411000285411%3B",
`s_sess` = "%20s_cc%3Dtrue%3B%20s_sq%3D%3B",
`s_vi` = "[CS]v1|29F92F108507B6C6-6000010A8000E2ED[CE]",
`ts-ca-one-locale` = "en_CA")
GET(url, set_cookies(.cookies = cookie), user_agent("Mozilla/5.0"))
This returns the same result.
However, when I look at the examples for httr I think the results I'm getting for setting the cookie are incorrect.
For example:
> example(set_cookies)
st_cks> set_cookies(a = 1, b = 2)
Config:
List of 1
$ cookie:"a1=;b2="
st_cks> set_cookies(.cookies = c(a = "1", b = "2"))
Config:
List of 1
$ cookie:"a1=;b2="
st_cks> GET("http://httpbin.org/cookies")
Response [http://httpbin.org/cookies]
Status: 200
Content-type: application/json
Size: 19 B
{
"cookies": {}
st_cks> GET("http://httpbin.org/cookies", set_cookies(a = 1, b = 2))
Response [http://httpbin.org/cookies]
Status: 200
Content-type: application/json
Size: 50 B
{
"cookies": {
"a1": "",
"b2": ""
}
I would have expected httpbin.org to return a = 1 and b = 1 instead of a1 = and b2 =. Am I doing something wrong?
You're not doing anything wrong (at least IMO). Taking a look at set_cookies, there's a bit of code there:
cookie <- paste0(names(cookies), cookies_str, sep = "=", collapse = ";")
which is not working as intended (i.e. it's turning a = "1" into a1= instead of a=1).
You can use this one:
sc2 <- function (..., .cookies = character(0)) {
cookies <- c(..., .cookies)
stopifnot(is.character(cookies))
cookies_str <- vapply(cookies, RCurl::curlEscape, FUN.VALUE = character(1))
cookie <- paste(names(cookies), cookies_str, sep = "=",
collapse = ";")
config(cookie = cookie)
}
GET("http://httpbin.org/cookies", sc2(a = 1, b = 2))
## Response [http://httpbin.org/cookies]
## Status: 200
## Content-type: application/json
## Size: 50 B
## {
## "cookies": {
## "a": "1",
## "b": "2"
## }
that uses paste instead of paste0 temporarily until that's fixed (I'll file an issue on the httr github repo).

Resources